user1227793 user1227793 - 1 year ago 126
CoffeeScript Question

ng-class does not change class unless I refresh the page

I'm using ng-class in my view to change the class of and element and it works fine on page load. Here is my code:

<li ng-class="'{{account.currency}}' == '{{currency}}' ? 'active': 'inactive'" ng-repeat="account in accounts" bind="accounts">
<a href="#/deposits/{{account.currency}}" ng-click="changeClass('{{account.currency}}', '{{currency}}')">

As you can see, it takes the second currency from the URL and compares it with another to decide which class it should assign to my li element.

The trouble now is that when someone clicks on the link and the URL changes, the class assigned does not change. Any idea how I can get the class assigned by ng-class to change when the

Answer Source

Try this,

var app = angular.module('app', []);

app.controller('myctrl', function() {
  var vm = this;
  vm.accounts = [{
    currency: 'Doller'
  }, {
    currency: 'Pound'
  }, {
    currency: 'Euro'
  vm.changeClass = function(account) { = account.currency;  
.active {
  background-color: green;
<script src=""></script>

  <div ng-app="app" ng-controller="myctrl as ct">
      <li ng-class="{'active': === account.currency}" ng-repeat="account in ct.accounts">
        <a href="#" ng-click="ct.changeClass(account)">


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download