AngularJS条件语句中的HTML属性 [英] AngularJS conditionals in HTML Attribute
问题描述
你将如何改变基于模型的 HTML
属性?
How would you change an html
attribute based on the model?
我想基于阵列的长度来改变输入的占位符文本:
I'm trying to change an input's placeholder text based on a length of an array:
<input placeholder="{{todos.length ? 'Insert todo' : 'Insert your first todo'}}" />
但是,这似乎并没有工作...
But that doesn't seem to work...
推荐答案
有关其他人谁遇到这样(像我刚刚通过谷歌所做的那样),它看起来像角最近增加了对前pressions三元运营商支持。我只是用它成功地在1.2.16动态更新提示(标题)属性。它似乎已经首次出现了1.2.17的文档中,虽然他们仍普遍鼓励使用:
For anyone else who comes across this (like I just did via Google), it looks like Angular recently added support for the ternary operator in expressions. I just used it successfully in 1.2.16 to dynamically update a tooltip (title) attribute. It seems to have first appeared in the documentation for 1.2.17, although they still generally discourage its use:
从:<一href=\"https://$c$c.angularjs.org/1.2.17/docs/guide/ex$p$pssion#no-control-flow-statements\">AngularJS:开发人员指南:防爆pressions
除了三元运算符(A B:C),你可以不写在一个前pression控制流的语句。这背后的原因是核心的角理念使应用程序逻辑应该在控制器,而不是意见。如果你需要一个真正的条件,循环,或从视图前pression扔,委托给一个JavaScript方法来代替。
Apart from the ternary operator (a ? b : c), you cannot write a control flow statement in an expression. The reason behind this is core to the Angular philosophy that application logic should be in controllers, not the views. If you need a real conditional, loop, or to throw from a view expression, delegate to a JavaScript method instead.
这篇关于AngularJS条件语句中的HTML属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!