微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

AngularJS条件在HTML属性

您将如何根据模型更改html属性

我正在根据数组的长度来更改输入的占位符文本:

<input placeholder="{{todos.length ? 'Insert todo' : 'Insert your first todo'}}" />

但这似乎不起作用

JS Bin code example.

对于遇到这个问题的任何人(像我刚刚通过Google一样),似乎Angular最近对表达式中的三元运算符添加支持.我在1.2.16中成功使用它来动态更新工具提示(title)属性.它似乎首先出现在1.2.17的文档中,尽管它们通常也不鼓励使用它:

来自:AngularJS: Developer Guide: Expressions

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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐