Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

javascript - 如何在Angular 2中添加条件属性?(How to add conditional attribute in Angular 2?)

How can I conditionally add an element attribute eg the checked of a checkbox?

(我如何能有条件地添加元素属性,例如checked复选框的?)

Previous versions of Angular had NgAttr and I think NgChecked which all seem to provide the functionality that I'm after.

(以前的Angular版本具有NgAttr ,我认为NgChecked似乎都提供了我所追求的功能。)

However, these attributes do not appear to exist in Angular 2 and I see no other way of providing this functionality.

(但是,这些属性在Angular 2中似乎不存在,我看不到提供此功能的其他方法。)

  ask by Jonathan Miles translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

null removes it:

(null将其删除:)

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

or

(要么)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...