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

查看:994
本文介绍了如何使用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天全站免登陆