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?
= or =attr - set up bi-directional binding between a local scope property and the parent scope property of name defined via the value of the attr attribute.
These prefixes are used to bind the parent scope's methods and properties to the directive scope. There are 3 types of prefixes in AngularJS: '@' – Text binding / one-way binding. '=' – Direct model binding / two-way binding.
The $scope in an AngularJS is a built-in object, which contains application data and methods. You can create properties to a $scope object inside a controller function and assign a value or function to it. The $scope is glue between a controller and view (HTML).
Inherited Scope This protects the parent controller's data while allowing it to be shared with the directive.
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).
"=?" 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With