[AngularJS]使用筛选器 '通过 $index 跟踪' 时我得到错误。如何解决这个问题?

发布时间: 2017/4/16 21:07:29
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有一个错误的数组值作为字符串。它有重复值。我想从数组字符串中移除不需要的空格和数字。所以我在清洗所有使用筛选器。

问题是,当我使用筛选器独自一人,我得到错误为 duplicates 如果消除了这些,我的筛选器不工作。

如何处理这种情况?

这里是我的代码︰

var myApp = angular.module('myApp',[]);

arr=[
  "1. Completion of Site mobilization \n2. Erection of Tower cranes 2, 4 & 5\n3. Facade dismantling  (Grey ACP between Gates 19, 23 & 24;  Steel Louvers between Gates 2 & 3)                                                                       \n4. Excavation for Foundation (AHU Corridor,  Foundation Area 1, 2  &3, External Buildings)\n5. Civil Works on East Expansion for foundation  (F50, F52, F55, F57, F94)\n6. West Stand Roof catwalks dismantling completed 100 %\n7. Assembly/Erection of temporary towers for Lighting Arch \n    dismantling\n8. Score Board dismantling\n9. Completion of Site mobilization 10. Erection of Tower cranes 2, 4 & 5\n11. Facade dismantling  (Grey ACP between Gates 19, 23 & 24;  Steel Louvers between Gates 2 & 3)                                                                       \n12. Excavation for Foundation (AHU Corridor,  Foundation Area 1, 2  &3, External Buildings)\n13. Civil Works on East Expansion for foundation  (F50, F52, F55, F57, F94)14. West Stand Roof catwalks dismantling completed 100 %\n15. Assembly/Erection of temporary towers for Lighting Arch \n    dismantling\n16. Score Board dismantling",
  "",
  ""
]


myApp.controller('main', function($scope){

    $scope.arrays = arr;

});

angular.module("myApp")
.filter("string2array", 

    function () {

        return function ( value ) {

            if(!value) return;

            return value.filter(Boolean).join('\n').replace(/^\d+\.\s+/gm, '').split('\n');

        }

});

这里是我 js 尝试︰Js 演示

这里是 angularjs 集成使得问题对我来说

angualrjs 演示

解决方法 1:

筛选器应用于 arrays 所以你只需要做...

<li ng-repeat="array in arrays | string2array track by $index">

提琴

赞助商