如何与JS的角度动态地设置一个HTML元素的id属性? [英] How to set the id attribute of a HTML element dynamically with angular js?

查看:130
本文介绍了如何与JS的角度动态地设置一个HTML元素的id属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提供类型的HTML元素 DIV ,如何设定其 ID 属性,它是价值一个范围的变量和一个字符串的串联?

Provided an HTML element of type div, how to set the value of its id attribute, which is the concatenation of a scope variable and a string ?

推荐答案

ngAttr 指令完全可以成为帮助在这里,作为正式文件出台

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

<一个href=\"https://docs.angularjs.org/guide/directive#-ngattr-attribute-bindings\">https://docs.angularjs.org/guide/directive#-ngattr-attribute-bindings

例如,设置 ID 属性的价值DIV 元素,所以它包含一个索引,视图片段可能包含

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

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

这将得到插值到

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

这篇关于如何与JS的角度动态地设置一个HTML元素的id属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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