2016-05-12 32 views
0

在我的應用程序我有一個條件納克級角納克級不斷變化的無功

INDEX.HTML 
<div class="card-panel" ng-click="update(1)" ng-class="selectedPromotion == 1 ? 'blue accent-3' : ''"> 
    Cas 1 
</div> 
<div class="card-panel" ng-click="update(2)" ng-class="selectedPromotion == 2 ? 'blue accent-3' : ''"> 
    Cas 2 
</div> 
<div class="card-panel" ng-click="update(3)" ng-class="selectedPromotion == 3 ? 'blue accent-3' : ''"> 
    Cas 3 
    </div> 

的方法更新設定的無功選擇

$scope.update = function(index){ 
    panierService.setChoice(index); 
}; 

在我的控制器的方法:

$scope.selectedPromotion = panierService.getChoice(); 

但是當我點擊不同的Div時,var被正確設置,但ng類停留在選擇1上,該方法不記得應用css風格

任何解決方案,請?

感謝您的幫助

回答

0

這裏的問題是,因爲$scope.selectedPromotion沒有該服務的價值相同的參考(panierService.getChoice()) ,因爲它們是數字而不是對象,並且panierService.setChoice方法可能會更改引用(如果它是一個對象)。您可以將$scope.selectedPromotion從數字更改爲返回panierService.getChoice的函數。此外,您可以將該變量重命名爲$scope.getSelectedPromotion

<div class="card-panel" ng-click="update(1)" ng-class="getSelectedPromotion() == 1 ? 'blue accent-3' : ''"> 
    Cas 1 
</div> 

<div class="card-panel" ng-click="update(2)" ng-class="getSelectedPromotion() == 2 ? 'blue accent-3' : ''"> 
    Cas 2 
</div> 
<div class="card-panel" ng-click="update(3)" ng-class="getSelectedPromotion() == 3 ? 'blue accent-3' : ''"> 
    Cas 3 
</div> 

並在控制器,你可以做

$scope.getSelectedPromotion = function() { 
    return panierService.getChoice(); 
}; 

$scope.update你可以離開像你一樣的方法。

0

你必須在變化更新您的變量:

$scope.update = function(index){ 
    panierService.setChoice(index); 
    $scope.selectedPromotion = index; //panierService.getChoice(); 
}; 
+0

這裏的問題是因爲您失去了服務值的一致性。也許setChoice可以創建其他邏輯,而不僅僅是設置索引。 –