ng-repeat使用相同的数组重复多次

时间:2016-09-08 10:25:07

标签: javascript angularjs

ng-repeat多次绑定相同的数组。

js:

    $scope.currentitem = item;
    $scope.currentitemCategory = $scope.currentitem.category.split(',');
    console.log($scope.currentitemCategory);

HTML:

   <div ng-repeat="category in currentitemCategory track by $index">
             <a href="/content/digital-library/us/en/search.html?category={{category}}">
            <span class="text"> {{category}} </span>
            </a>
                                         </div>

console:

分类                                                                                               audience/business                                                                                                audience/business                                                                                                audience/business                                                                                                brandguidelines                                                                                                brandguidelines                                                                                                brandguidelines                                                                                                corporateinitiatives/idf                                                                                                corporateinitiatives/idf                                                                                                corporateinitiatives/idf                                                                                       

1 个答案:

答案 0 :(得分:1)

我已经使用您指定的代码段创建了一个示例应用程序。我没有看到任何重复的条目。一切正常。如果问题确实存在,请提供更多详细信息。

&#13;
&#13;
var app= angular.module('sample', []);

app.controller('samplecontroller', function($scope){
   var item = {category: 'Tennis, Carroms, Soccer, Volleyball'};
   $scope.currentitem = item;
   $scope.currentitemCategory = $scope.currentitem.category.split(',');
   console.log($scope.currentitemCategory);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="sample">
  <div ng-controller="samplecontroller">
    <div ng-repeat="category in currentitemCategory track by $index">
      <a href="/content/digital-library/us/en/search.html?category={{category}}">
        <span class="text"> {{category}} </span>
      </a>
    </div>
  </div>

</body>
&#13;
&#13;
&#13;