When and why to use &?, =?, @? in AngularJS?

Shaohao Lin picture Shaohao Lin · Jul 23, 2015 · Viewed 13.3k times · Source

I followed some tutorials to create angular directives. In the isolate scope, some tutorials define scope like this:

scope: {
  model: '=?',
  data:  '@?' 
}

Meanwhile, some tutorials define scope without question mark like this:

scope: {
  model: '=',
  data:  '@'
}

Can anybody explain me the difference or the purpose of these with examples? Thank you.

Answer

Daniel Cottone picture Daniel Cottone · Jul 23, 2015

The &, @, and = symbols are used to define the bindings (one-way, bi-directional, etc) for isolated scope objects, as you already know. Here is a pretty thorough tutorial on how all this works.

The ? symbol is used to indicate that the parent scope property to which the isolated scope binding refers to is optional. This means that if for some reason the parent scope property doesn't exist, then your application will continue to run without throwing the NON_ASSIGNABLE_MODEL_EXPRESSION exception.