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

查看:43
本文介绍了如何使用 angularjs (1.x) 动态设置 HTML 元素的 id 属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提供一个 div 类型的 HTML 元素,如何设置它的 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

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

例如,要设置 div 元素的 id 属性值,使其包含索引,则视图片段可能包含

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>

这篇关于如何使用 angularjs (1.x) 动态设置 HTML 元素的 id 属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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