I have read a lot of articles explaining the difference between @
, =
and &
. I've seen a lot of people using =?
in their code. What does this mean?
Unfortunately, I can't seem to search on Google or SO for an answer because search engines ignore special characters.
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.
Property binding is used to bind the data from property of a component to DOM elements. It is denoted by []. Let's understand with a simple example.
All three bindings are ways of passing data from your parent scope to your directive's isolated scope through the element's attributes: @ binding is for passing strings. These strings support {{}} expressions for interpolated values.
= 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.
The ?
makes the property optional. Otherwise, you'll get NON_ASSIGNABLE_MODEL_EXPRESSION
where Angular is looking for a property that doesn't exist.
From the docs:
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.
https://docs.angularjs.org/api/ng/service/$compile#-scope-
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