Angular2-动态添加HTML属性和类 [英] Angular2 - add HTML attributes and classes dynamically

查看:446
本文介绍了Angular2-动态添加HTML属性和类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我找到有关如何动态添加属性&&的答案.类到Angular 2中的HTML模板?

Could you please help me to find an answer of how to dynamically add an attribute && class to the HTML template in Angular 2?

例如:

<div class="some-class class-8" col-8></div>

上面示例中的

8应该来自配置属性,但是如果未定义特定属性,则不应注入 col- class-.

请不要指出这种类型的属性是一个错误(这种情况发生在Ionic2中)

Please do not point out that this type of attribute is a bug (this case occurs in Ionic2)

推荐答案

[attr.col-8]是正确的语法,但条件应返回空字符串(true,属性已设置)或null(false,attribute为没有设置). 例如:

[attr.col-8] is the right syntax, but the condition should return empty string (true, attribute is set) or null (false, attribute is not set). For example:

[attr.col-6]="field.width == 6 ? '' : null"

工作正常. (我正在使用离子3)

does work fine. (i am working with ionic 3)

这篇关于Angular2-动态添加HTML属性和类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆