【问题标题】:AngularJS - original index of an object within filtered ng-repeatAngularJS - 过滤后的 ng-repeat 中对象的原始索引
【发布时间】:2015-04-07 23:16:03
【问题描述】:

我在对象上使用嵌套的 ng-repeat 和过滤器。第一个 ng-repeat 是对 gapHeader 对象中的 headerId 的过滤器。第二个 ng-repeat 过滤 gapSection、sectionId 到对应的 headerID。

我有一个编辑页面,它位于一个单独的模式窗口中。目的是编辑子对象的headerID和sectionID对应的内容)这个也有单独的控件。数据通过服务共享。

我的问题我的每个 gapSection 子对象都有一个按钮,它打开编辑页面模式,当我将每个部分中当前部分的 $index 值传递给服务时,我得到的 $index 只对应于第二次重复?例如,如果我在 gapSection (headerId:2, sectionId:2) 上单击 2 ng-repeat 中的按钮,我会得到一个 $index 1。我需要一个 $index 2,它对应于 gapSection 中的子对象位置.

是否可以传递真正的 $index,它对应于 gapSection 的原始未过滤对象中定义的 $index?感谢任何关于此的 cmets,谢谢!

对象:

var data ={
gapHeader:[{headerId:1,name:"General Requiremets",isApplicable:true},
                    {headerId:2,name:"Insurance",isApplicable:false}],


gapSection:[{headerId:1,sectionId:1,requirement:"A facility is required to have company structure",finding:null,cmeasures:null,cprocedures:null,personResp:null,isAction:null},
        {headerId:2,sectionId:1,requirement:"Organisation must have public liablity",finding:null,cmeasures:null,cprocedures:null,personResp:null,isAction:null},
        {headerId:2,sectionId:2,requirement:"Facility must hold workers compensation insurance",finding:null,cmeasures:null,cprocedures:null,personResp:null,isAction:null}]



};

【问题讨论】:

标签: angularjs


【解决方案1】:

如果您需要真正的索引,您甚至不需要传递$index 属性,只需传递对象并从原始列表中获取索引即可。

$scope.gapSectionFn = function(obj){
    var idx = data.gapSection.indexOf(obj);
}

还不清楚您的问题是否真的是嵌套的 ng-repeat 问题,因为根据您的说法 gapSection 是内部 ng-repeat 并且您正在从内部 ng-repeat 调用调用并且需要 gapSection's 索引.它应该是可用的,但是 DOM 过滤器的存在只会重新组织项目及其索引,您也可以通过执行 ng-init 获得,即在视图 ng-init="actIndex=$index" 上并使用 @ 987654327@.

如果你试图访问父 ng-repeat 的索引,那么 ng-init 比 $parent.$index 更合适。由于 ng-init 是专门为此设计的。在父 ng-repeat 上,您将编写 ng-init=""parentIndex=$index" 并使用 parentIndex。

【讨论】:

  • indexOf(obj) 完美运行,谢谢!你对 ng-repeat 是正确的,真正的第一个 ng-repeat 适用于不同的对象,因此为什么我最初尝试的 $parent.$index 不起作用,因为我得到的是 gapHeader 的索引而不是 GapSection。再次感谢,我认为这比想象的要难得多!
猜你喜欢
  • 1970-01-01
  • 2019-03-12
  • 1970-01-01
  • 2014-06-11
  • 1970-01-01
  • 2015-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多