2015-04-23 50 views
0

我想在我的應用程序中添加類ng-class如何設置類在我的情況

我有類似

HTML

<li ng-repeat="item in items" 
     ng-click="getItem(item)" 
     ng-class="{shine : item.isPick}" // shine class will make the font color change to red 
     >{{item.name}} 
</li> 

在我的控制器

$scope.getItem = function(item) { 
    $scope.items.forEach(function(item){ 
     item.isPick = false; // remove all the items that have shine class 
    }) 
    item.isPick = true; // adding the shine class back 
} 

基本上我想,當用戶點擊它來除去所有其他 'shine' 級,除了所選擇的項目。我的代碼上面的工作,但我認爲有一個更好的方式來做到這一點,而不是循環所有的項目。有人可以幫助我嗎?非常感謝!

回答

3

使用$index在轉發,並添加時指數選擇了類:

<li ng-repeat="item in items" 
    ng-click="getItem($index)" 
    ng-class="{shine : activeIndex == $index}" 
    >{{item.name}} 
</li> 

$scope.getItem = function(index) { 
    $scope.activeIndex = index; 
} 
相關問題