如果你有一个包含ng-repeat的指令,你想修改"跟踪"或" as" ng-repeat怎么做而不在模板中创建两个单独的ng-repeats?我希望能够通过ng-repeat作为指令属性传入轨道。所以在字符串数组的情况下,它就像是repeat-track-by =" row"在对象数组的情况下,它将是repeat-track-by =" row.title"。有没有办法实现这个目标?
angular.module("app", []);
angular.module("app")
.controller("appCtrl", function($scope) {
$scope.sampleData = ["sample 1", "sample 2", "sample 3"];
$scope.sampleData2 = [{title:"sample 1"}, {title:"sample 2"}, {title:"sample 3"}];
})
angular.module("app")
.directive("repeatDirective", function() {
return {
restrict: 'E',
scope: {
data: '='
},
template: "<div ng-repeat='row in data track by row'>{{row}}</div>",
link: function (scope) {}
}
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="app">
<div ng-controller="appCtrl">
<repeat-directive data="sampleData"></repeat-directive>
</div>
</body>
&#13;