Ravi Sahu
Ravi Sahu

Reputation: 910

Angular: what is the difference between '=' and '=?' while defining a scope in a directive

I am just picking up selectize example

    angular.module('selectize', []).value('selectizeConfig', {}).directive("selectize", ['selectizeConfig', function(selectizeConfig) {
      return {
        restrict: 'EA',
        require: '^ngModel',
        scope: { 
          ngModel: '=', 
          config: '=?', 
          options: '=?', 
          ngDisabled: '=', 
          ngRequired: '&' 
        },
link: function(scope, element, attrs, modelCtrl) {...}

I know '=' will map existing controller scope's property in two way but what about '=?' ?

Upvotes: 1

Views: 68

Answers (2)

S M
S M

Reputation: 3233

scope: {foo: '=?'}

means 'do not raise error if 'foo' cannot be resolved.

The 'isolate' scope takes an object hash which defines a set of local scope properties derived from the parent scope. These local properties are useful for aliasing values for templates. Locals definition is a hash of local scope property to its source:

= 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. If no attr name is specified then the attribute name is assumed to be the same as the local name. Given and widget definition of

scope: {  
localModel:'=myAttr' }, 

then widget scope property localModel will reflect the value of parentModel on the parent scope. Any changes to parentModel will be reflected in localModel and any changes in localModel will reflect in parentModel. 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.

Upvotes: 2

Satpal
Satpal

Reputation: 133423

= or =attr set's up bi-directional binding between a local scope property and parent scope property, If the parent scope property doesn't exist, it will throw a NON_ASSIGNABLE_MODEL_EXPRESSION exception.

This can avoid this behavior using =? or =?attr in order to flag the property as optional

Upvotes: 2

Related Questions