如何在Angular 2中添加条件属性? [英] How to add conditional attribute in Angular 2?

查看:450
本文介绍了如何在Angular 2中添加条件属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何有条件地添加元素属性,例如复选框的已选中

How can I conditionally add an element attribute e.g. the checked of a checkbox?

以前版本的Angular有 NgAttr 我觉得 NgChecked 这些似乎都提供了我所追求的功能。但是,这些属性似乎不存在于Angular 2中,我看不到提供此功能的其他方式。

Previous versions of Angular had NgAttr and I think NgChecked which all seem to provide the functionality that I'm after. However, these attributes do not appear to exist in Angular 2 and I see no other way of providing this functionality.

推荐答案

null 将其删除:

[attr.checked]="value ? '' : null"

[attr.checked]="value ? 'checked' : null"

这篇关于如何在Angular 2中添加条件属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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