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
401 views
in Technique[技术] by (71.8m points)

javascript - 如何使用angularjs(1.x)动态设置HTML元素的id属性?(How to set the id attribute of a HTML element dynamically with angularjs (1.x)?)

提供了一个div类型的HTML元素,如何设置其id属性的值,这是范围变量和字符串的串联?

  ask by Thierry Marianne translate from so

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

1 Reply

0 votes
by (71.8m points)

ngAttr directive can totally be of help here, as introduced in the official documentation

(ngAttr官方文档中所介绍的那样, ngAttr指令在这里可以完全有所帮助)

https://docs.angularjs.org/guide/interpolation#-ngattr-for-binding-to-arbitrary-attributes

(https://docs.angularjs.org/guide/interpolation#-ngattr-for-binding-to-arbitrary-attributes)

For instance, to set the id attribute value of a div element, so that it contains an index, a view fragment might contain

(例如,要设置div元素的id属性值,以便它包含索引,视图片段可能包含)

<div ng-attr-id="{{ 'object-' + myScopeObject.index }}"></div>

which would get interpolated to

(将被内插到)

<div id="object-1"></div>

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

...