Reputation: 269
scope: {
someProperty: "=?"
somePropertyTwo: =?Wheeeeee
}
What does "=?" do? I can't find the answer anywhere. I understand that using scope: {} (or scope: true) gives the directive a new scope, with the former being an isolate scope and the latter being one-way binded to parent Ctrl (Ctrl of the page/view on which the directive is used). I understand that:
someProp: @X //will one-way bind someProp to parent Ctrl's X
someProp: =X //two-way
someProp: &X() //some space magic for binding methods
However, I don't understand how/why " =? " is used.
Relevant articles (that only cover the first three): http://www.undefinednull.com/2014/02/11/mastering-the-scope-of-a-directive-in-angularjs/ http://weblogs.asp.net/dwahlin/creating-custom-angularjs-directives-part-2-isolate-scope
edit: Is it the same as these?
? - Attempt to locate the required controller or pass null to the link fn if not found. ^ - Locate the required controller by searching the element and its parents. Throw an error if not found. https://docs.angularjs.org/api/ng/service/$compile#-require-
I.e. null is passed if whatever property isn't found?
Upvotes: 12
Views: 13029
Reputation: 19
Directives in AngularJS are very powerful, but it takes some time to understand what processes lie behind them. While creating directives, AngularJS allows you to create an isolated scope with some custom bindings to the parent scope. These bindings are specified by the attribute defined in HTML and the definition of the scope property in the directive definition object.
There are 3 types of binding options which are defined as prefixes in the scope property. The prefix is followed by the attribute name of HTML element. These types are as follows
Text Binding (Prefix: @) One-way Binding (Prefix: &) Two-way Binding (Prefix: =)
Upvotes: 1
Reputation: 14114
It just means a two-way binding is optional. If you define a property by using =
then you must provide it with a valid binding. From $compile
documentation:
(...) If the parent scope property doesn't exist, it will throw a NON_ASSIGNABLE_MODEL_EXPRESSION exception. You can avoid this behavior using =? or =?attr in order to flag the property as optional. If you want to shallow watch for changes (i.e. $watchCollection instead of $watch) you can use =* or =attr (=? or =*?attr if the property is optional).
Upvotes: 14
Reputation: 1367
"=?" is just optional "=".
They are identical except that if you miss this property when you use this directive, no error will occur, the scope will be used as normal internally.
Upvotes: 7