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

具有ng-repeat的Angularjs为相同类型的多个元素生成相同的id

发布时间:2020-12-17 07:07:19 所属栏目:安全 来源:网络整理
导读:我有一个html页面,我有一个对象’Message’,它有一个属性’Properties’,它是一个对象数组.问题是,当我使用’ng-repeat’迭代’Message.Properties’项目来渲染我使用 angularjs创建的某些自定义元素时,它又生成一个id,我获得相同的id的id类型.这是我的代码
我有一个html页面,我有一个对象’Message’,它有一个属性’Properties’,它是一个对象数组.问题是,当我使用’ng-repeat’迭代’Message.Properties’项目来渲染我使用 angularjs创建的某些自定义元素时,它又生成一个id,我获得相同的id的id类型.这是我的代码示例:

<div data-ng-repeat="property in Message.Properties">
...
  <div ngm-if="(property.Type == 9)">                            
    <sdk:selectbox name="property.Title" row="" datafield="property.Value"
      options="property.ValueCollection" displayfield="Value" valuefield="Key"
      mandatory="property.isMandatory" labeled="property.Title" multiple="">
    </neolynk:selectbox>
  </div>
</div>

重新构造,当编译元素“sdk:selectbox”时,如果我在具有相同类型的Message.Properties中有2个属性(例如:9),则生成的id(创建的html输入)对于两者都是相同的(虽然我随机生成它,但我当然需要不同的Ids用于不同的输入.我读到ng-repeat为每个项目制作模板的克隆,是吗?!怎么解决?我将不胜感激任何帮助.

解决方法

您可以在ng-repeat中使用$index来创建基于当前行的唯一ID.摘自 the docs:

Special properties are exposed on the local scope of each template
instance,including:

  • $index – {number} – iterator offset of the repeated element (0..length-1)
  • $first – {boolean} – true if the repeated element is first in the iterator.
  • $middle – {boolean} – true if the repeated element is between the first and last in the iterator.
  • $last – {boolean} – true if the repeated element is last in the iterator.

(编辑:李大同)

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

    推荐文章
      热点阅读