Lionel B Lionel B - 1 year ago 88
Javascript Question

Angular ng-class with changing var

In my app i have a condition ng-class

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

The method update set the var choice

$scope.update = function(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 Source

You've got to update your variable on change:

$scope.update = function(index){
    $scope.selectedPromotion = index; //panierService.getChoice();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download