Lionel B Lionel B - 6 months ago 16
Javascript Question

Angular ng-class with changing var

In my app i have a condition ng-class

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>


The method update set the var choice

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


The method in my controller :

$scope.selectedPromotion = panierService.getChoice();


But when i click on the different Div , the var is correctly set but the ng-class stay on the choice 1 , the method is not recall to apply the css Style

Any solution please ?

Thanks for your help

Answer

You've got to update your variable on change:

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