1

For my below code ng-class isn't working:

HTML

<div class="row-content" ng-controller="MainController">
  <div class="tabs">
    <div class="col-xs-6">
      <h3 ng-class="{selected: searchType == 'news'}">
        <a ng-click="SetSearchType('news')" 
           href="javascript:void(0)">Alumni News</a>
      </h3>
    </div>
    <div class="col-xs-6">
      <h3 ng-class="{selected: searchType == 'rss'}">
        <a ng-click="SetSearchType('rss')" 
           href="javascript:void(0)">Firm News</a>
      </h3>
    </div>
  </div>
</div>

AnjgularJS

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

app.controller('MainController', ['$scope', function($scope) {

    $scope.searchType = 'news';

    $scope.SetSearchType = function(type) {
        $scope.searchType = type;
    }
}]);
13
  • 1
    Try in quotes: ng-class="{'selected': searchType === 'news'}" Commented Sep 14, 2017 at 9:07
  • 1
    Are you sure your view is using that controller? Commented Sep 14, 2017 at 9:07
  • Can you please create a Minimal, Complete, and Verifiable example? Commented Sep 14, 2017 at 9:07
  • I think the problem is not in the code you posted, I reproduced it in this plunker and it's working ! Commented Sep 14, 2017 at 9:11
  • any error u getting in console ? Commented Sep 14, 2017 at 9:15

2 Answers 2

1

I think there is something wrong with your CSS styles or your $scope.selected param. Please check this runnable demo fiddle and carefully compare it with your solution:

View

<div class="row-content" ng-controller="MainController">
  <div class="tabs">
    <div class="col-xs-6">
      <h3 ng-class="{'selected': searchType == 'news'}">
        <a ng-click="SetSearchType('news')" 
           href="javascript:void(0)">Alumni News</a>
      </h3>
    </div>
    <div class="col-xs-6">
      <h3 ng-class="{'selected': searchType == 'rss'}">
        <a ng-click="SetSearchType('rss')" 
           href="javascript:void(0)">Firm News</a>
      </h3>
    </div>
  </div>
</div>

AngularJS

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

myApp.controller('MainController', function($scope) {
  $scope.searchType = 'news';

  $scope.SetSearchType = function(type) {
    $scope.searchType = type;
  }
});

CSS

.selected a {
  color:red;
}
Sign up to request clarification or add additional context in comments.

1 Comment

ngClass doesn't need quote, even without, it works. Take a look at this plunker
0

put your class in quotes ('')

<div class="row-content" ng-controller="MainController">
                <div class="tabs">
                    <div class="col-xs-6">
                        <h3 ng-class="{'selected': searchType == 'news'}">
                            <a ng-click="SetSearchType('news')" href="javascript:void(0)">Alumni News</a>
                        </h3>
                    </div>
                    <div class="col-xs-6">
                        <h3 ng-class="{'selected': searchType == 'rss'}">
                            <a ng-click="SetSearchType('rss')" href="javascript:void(0)">Firm News</a>
                        </h3>
                    </div>
                </div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.