I am learning angular js and have now a question where I couldn't find the right answer yet.
in the template HTML, you can use expressions to show the scope variables or call scope functions. But I see all the time different versions of it.
{{name}}
shows the variable and binds it
{{::name}}
the same thing but without binding
userdirective="{{::key}}"
But what is the difference here?
ng-if="::field.sortable"
With ng-if they are not using {{ but with there userdirective they do?
userdirective="{condition:isActive(route.name),mdColors:{color:'primary'}}"
And then there is the last one with just one {. Thats when you create an object.right?
Maybe someone can help me to understand all of it. Thank you very much for your time. Pat
{{name}}
as you say is two-way data-binding
{{::name}}
one way databinding
userdirective="{{::key}}"
is the interesting case. This statement uses one-way binding into the userdirective ... which means after the $digest
it just says userdirective="someValue"
So the userdirective gets that value as a plain value. Now I would have to test it but in the scope
part of the directiive it should say @
so it gets read as a string and not as a expression.
The last one is simply as any JSON you build
{ name: value?true:false }
setting value according to conditions that angular evaluates, with a bit of magic involved :D
hope that helps
{{ anything here}}
- That is angular expression interpolation. Angular interpolation - here you can find more about that. Basically idea that it interpolate anything you will put inside those brackets. So if you will put expression with some calculations or just variables related to current scope it will convert all variables to their values and apply calculations.
For instance: {{scopevar1 + scopevar2}}
in case this variables has some values, let it be 1
and 2
, as result we will see 3
.
::
- This mean one time binding
. For instance {{::scopevar1}}
it will be interpolated once and will not check for changes of scopevar1
, always stay as first value. Even if scopevar1
will change every second, the value in template will be the same. Angular Expressions - here you can find some live examples and more information.
userdirective="{{::key}}"
- This case is nothing more then assigning dynamic value to your directive. UserDirective
expectes to get a simple value, but we have it inside our scope
, so we need to say: Hey, angular please interpolate scope variable - key
, but only once, so my directive will get value, and will not looking for updates of key
. And angular does it with pleasure!
userdirective="{condition:isActive(route.name),mdColors:{color:'primary'}}"
The last case is when your directive expects to get some kind of specific JSON. And we don't want to build it inside of controller. It is sometimes easier to do such things in the tempalte. So we put specific object with two properties: condition, mdColors
. And saying that first property assigned to result of function, and second one is simple object {color:'primary'}
.
That's it!
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