你能在ng-repeat之外使用$ index吗?

时间:2013-06-06 05:48:32

标签: angularjs angularjs-scope angularjs-ng-repeat

像这样:

<div class="item" ng-repeat="priority in priorities" ng-class="{active: {{$index}} == {{routeParams.priorityId - 1}} }"></div>

<a class="carousel-control left" ng-hide="!goals.length" href="#myCarousel" ng-click="changePriorityIdParam($index)" data-slide="prev">&lsaquo;</a>

app.controller("PriorityDetailController", function ($http, $scope, $location, $routeParams) {
    console.log("PriorityDetailController - priorityId: " + $routeParams.priorityId);
    getPriorityForPriorityId($http, $scope, $routeParams);

    $scope.changePriorityIdParam = function(index) {
        console.log("changePriorityIdParam() - index: " + index);
        $location.path('/priority/' + index);
        $location.replace();
    };
})

1 个答案:

答案 0 :(得分:0)

不,这没有任何意义。你要复制哪个索引,第一个,最后一个,所有这些索引?你正在做那个旋转木马真的错了。

在prevites集合上编写一个过滤器,它将获得您的活动元素。

或类似的东西:

$scope.changePriorityIdParam = function() {
    var index = 0;
    angular.forEach($scope.priorities, function ( priority, idx) {
        if ( priority == routeParams.priorityId - 1 ) {
            index = idx;
        }
    });
    console.log("changePriorityIdParam() - index: " + index);
    $location.path('/priority/' + index);
    $location.replace();
};