string interpolation in angular 2 and it's dynamicity

强颜欢笑 提交于 2019-12-08 03:51:23

问题


In Angular2, why if i put a property in a simple view, like this:

<span>{{allowServer}}</span>

it change whenever its value change on its .ts file, and if i put it like this:

<button class="btn btn-primary" disabled={{allowServer}} >server</button>

the button doesn't get the new value disabled?

So, what is the rule for which I have to use interpolation instead of binding syntax?

[disabled]=allowServer 

回答1:


[prop]="value"

is for object binding to properties (@Input() of an Angular component or directive or a property of a DOM element).

prop="{{value}}"

binds a value to a property. The value is stringified (aka interpolation)

See lets look at an example:

<button id="button1" disabled={{allowServer}} >server</button>
<button id="button2" [disabled]={{allowServer}} >server</button>

1) allowServer === true

button1.disabled = 'true' // string

button2.disabled = true   // boolean

2) allowServer === false

button1.disabled = 'false' // string so disabled is true

button2.disabled = false// boolean

As you can see button1 will always be disabled



来源:https://stackoverflow.com/questions/46509364/string-interpolation-in-angular-2-and-its-dynamicity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!