Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

" =? " in Angular Directive Isolate Scope

Tags:

angularjs

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?

like image 782
Whee Avatar asked Apr 10 '15 01:04

Whee


People also ask

What is =? In AngularJS?

= 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.

What is @? In Angular directive scope?

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.

What is Angular $scope?

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).

What type of scope allows a directive to be re used within a given scope?

Inherited Scope This protects the parent controller's data while allowing it to be shared with the directive.


2 Answers

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).

like image 68
Michael Benford Avatar answered Oct 25 '22 05:10

Michael Benford


"=?" 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.

like image 43
Tong Shen Avatar answered Oct 25 '22 05:10

Tong Shen