-
Notifications
You must be signed in to change notification settings - Fork 27.3k
docs($compile): add more info about optional bindings #16025
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -303,21 +303,22 @@ | |
| * name. Given `<my-component my-attr="parentModel">` and the isolate scope definition `scope: { | ||
| * localModel: '=myAttr' }`, the property `localModel` on the directive's scope will reflect the | ||
| * value of `parentModel` on the parent scope. Changes to `parentModel` will be reflected in | ||
| * `localModel` and vice versa. Optional attributes should be marked as such with a question mark: | ||
| * `=?` or `=?attr`. If the binding expression is non-assignable, or if the attribute isn't | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This info sounds correct too. Why remove it?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not correct: http://plnkr.co/edit/AU3n1j9SPhx9FErCyvfj?p=preview
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is correct: http://plnkr.co/edit/7x6kyfbpWIFCFgTmZb2Z?p=preview
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right, thanks. This is a more specific case than is handled by the PR. |
||
| * optional and doesn't exist, an exception ({@link error/$compile/nonassign `$compile:nonassign`}) | ||
| * will be thrown upon discovering changes to the local value, since it will be impossible to sync | ||
| * them back to the parent scope. By default, the {@link ng.$rootScope.Scope#$watch `$watch`} | ||
| * `localModel` and vice versa. If the binding expression is non-assignable, or if the attribute | ||
| * isn't optional and doesn't exist, an exception | ||
| * ({@link error/$compile/nonassign `$compile:nonassign`}) will be thrown upon discovering changes | ||
| * to the local value, since it will be impossible to sync them back to the parent scope. | ||
| * | ||
| * By default, the {@link ng.$rootScope.Scope#$watch `$watch`} | ||
| * method is used for tracking changes, and the equality check is based on object identity. | ||
| * However, if an object literal or an array literal is passed as the binding expression, the | ||
| * equality check is done by value (using the {@link angular.equals} function). It's also possible | ||
| * to watch the evaluated value shallowly with {@link ng.$rootScope.Scope#$watchCollection | ||
| * `$watchCollection`}: use `=*` or `=*attr` (`=*?` or `=*?attr` if the attribute is optional). | ||
| * `$watchCollection`}: use `=*` or `=*attr` | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whyyyy??? 😢
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like above, the info about optional attributes is further down. Note that the &,
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough. |
||
| * | ||
| * * `<` or `<attr` - set up a one-way (one-directional) binding between a local scope property and an | ||
| * expression passed via the attribute `attr`. The expression is evaluated in the context of the | ||
| * parent scope. If no `attr` name is specified then the attribute name is assumed to be the same as the | ||
| * local name. You can also make the binding optional by adding `?`: `<?` or `<?attr`. | ||
| * local name. | ||
| * | ||
| * For example, given `<my-component my-attr="parentModel">` and directive definition of | ||
| * `scope: { localModel:'<myAttr' }`, then the isolated scope property `localModel` will reflect the | ||
|
|
@@ -347,6 +348,36 @@ | |
| * and values into the expression wrapper fn. For example, if the expression is `increment(amount)` | ||
| * then we can specify the amount value by calling the `localFn` as `localFn({amount: 22})`. | ||
| * | ||
| * All 4 kinds of bindings (`@`, `=`, `<`, and `&`) can be made optional by adding `?` to the expression. | ||
| * The marker must come after the mode and before the attribute name. | ||
| * See the {@link error/$compile/iscp Invalid Isolate Scope Definition error} for definition examples. | ||
| * This is useful to refine the interface directives provide. | ||
| * One subtle difference between optional and non-optional happens **when the binding attribute is not | ||
| * set**: | ||
| * - the binding is optional: the property will not be defined | ||
| * - the binding is not optional: the property is defined | ||
| * | ||
| * ```js | ||
| *app.directive('testDir', function() { | ||
| return { | ||
| scope: { | ||
| notoptional: '=', | ||
| optional: '=?', | ||
| }, | ||
| bindToController: true, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't you need a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope: #15110
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh...you just can't use it on the template. Cool 😃 |
||
| controller: function() { | ||
| this.$onInit = function() { | ||
| console.log(this.hasOwnProperty('notoptional')) // true | ||
| console.log(this.hasOwnProperty('optional')) // false | ||
| } | ||
| } | ||
| } | ||
| }) | ||
| *``` | ||
| * | ||
| * | ||
| * ##### Combining directives with different scope defintions | ||
| * | ||
| * In general it's possible to apply more than one directive to one element, but there might be limitations | ||
| * depending on the type of scope required by the directives. The following points will help explain these limitations. | ||
| * For simplicity only two directives are taken into account, but it is also applicable for several directives: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The suggestion about optional attributes seems correct. Why remove it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've removed it because I centralized the info after the individual descriptions.