加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 为“ng-repeat”中的每个项目赋予唯一ID以进行DOM

发布时间:2020-12-17 10:26:12 所属栏目:安全 来源:网络整理
导读:.div(ng-repeat='item in items') button(id='ID{{$index}}') No Hibutton(id='anotherID1') Hi 内部相关的角度指令 $('#ID1').on('click',function() {alert('hi');}); // does not work $('#anotherID1').on('click',function() {alert('hi');}); // works
.div(ng-repeat='item in items') 
   button(id='ID{{$index}}') No Hi

button(id='anotherID1') Hi

内部相关的角度指令

$('#ID1').on('click',function() {alert('hi');});  // does not work 

$('#anotherID1').on('click',function() {alert('hi');}); // works

我正在执行DOM操作,我需要ng-repeat元素的唯一ID.还请建议是否可以以任何其他方式执行DOM操作.

编辑:@tymeJV: –

style.
  .ques {
    height: 50px;
    overflow:hidden;
  }



<div ng-repeat='item in items'>
    <div class='ques'> {{item.ques}}
    </div>
    <button id='ID{{$index}}'> No Hi </button>
</div>

//指令代码: – 增加< div class ='ques'>的高度

首先,如果您使用的是Angular,建议您为侦听器坚持使用标准的Angular指令,而不是恢复为jQuery.因此,不要使用jQuery,而是在希望Angular绑定侦听器的HTML事件上使用ng-click属性.

例如:

HTML:

<button ng-click="doStuff()">
</button>

控制器:

$scope.doStuff = function () {
    //perform action
};

为了存储由ng-repeat创建的每个元素的唯一ID,如何使用ID:ng-click =“doStuff(item.ID)”向doStuff调用添加参数,并在$scope.doStuff方法中访问它.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读