0

I want to change the class name of div using angularjs (app.js) I followed this answer but the class name is not changing in my view.

Here is my view

<div ng-class="class">{{class}}</div>
<button ng-click="changeClass()">Change Class</button>

Here is my app.js

var app = angular.module('myApp', ['ngRoute', 'ngAnimate', 'toaster']);

app.controller("myApp", function ($scope) {
    $scope.class = "red";
    $scope.changeClass = function () {
        alert('a');
        if ($scope.class === "red")
            $scope.class = "blue";
        else
            $scope.class = "red";
    };
});
app.config(['$routeProvider',
        function ($routeProvider) {
            $routeProvider.
            when('/', {
                    title: 'Dashboard',
                    templateUrl: 'views/dashboard.html',
                    controller: 'authCtrl',
                    role: '0'
                })
                .otherwise({
                    redirectTo: '/login'
                });
        }
    ])
    .run(function ($rootScope, $location, Data) {
        $rootScope.$on("$routeChangeStart", function (event, next, current) {
            var nextUrl = next.$$route.originalPath;
            if (localStorage.getItem("serviceUserId") === null) {
                $location.path("/login");
            } else {

                if (nextUrl == '/') {
                    $location.path("/dashboard");
                }
            }
        });
    });

What is the mistake i am doing and how can i fix it ?

8
  • 1
    Try class="{{class}}" Commented Nov 5, 2015 at 11:46
  • Try ng-class="{red: isRed, blue: isBlue}" and $scope.isRed=!$scope.isRed; $scope.isBlue=!$scope.isRed; in your changeClass Commented Nov 5, 2015 at 11:47
  • Is there any console error? Did you ng-app and ng-controller Commented Nov 5, 2015 at 11:47
  • for me it's working plunker Commented Nov 5, 2015 at 11:56
  • @Tushar I changed to {{class}} still it is not working Commented Nov 5, 2015 at 12:08

2 Answers 2

2

Note: Your code works for me w/o any modifications.

Following should work also:

<div ng-class="{red: className === 'red', blue: className === 'blue'}">{{className}}</div>

Also, you can make it simpler with class="{{className}}"


I use className everywhere, because class is js-reserved word.
Some minifiers will fail with it, for example.

Sign up to request clarification or add additional context in comments.

6 Comments

So, i should use like this ? <div ng-class="{{class}}">{{className}}</div> ?
No, class="{{className}}" or ng-class="className". Look for other error in your code/devConsole. That part is correct.
i noted that even alert('a'); is not triggering for me .
Check it in the original fiddle: jsfiddle.net/geonunez/pWG2S If it not works - issue with your browser.
Aha, your latest chrome can be infected by anything, that inject some code for example... Are you check if part of my comment above?
|
1

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

app.controller("myCtrl", function ($scope) {
    $scope.className = "red";
    $scope.changeClass = function () {
        alert('a');
        if ($scope.className === "red")
            $scope.className = "blue";
        else
            $scope.className = "red";
    };
});
.red {
  background-color: red;
}

.blue {
  background-color: blue;
}

.red, .blue {
  width: 200px;
  height: 200px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>

<div ng-app="myApp">
  <div ng-controller="myCtrl">
    <div class="{{className}}">{{className}}</div>
    <button ng-click="changeClass()">Change Class</button>
  </div>
</div>

2 Comments

Recheck it now. I've made a snippet
The issue was fixed after fiving myCtrl !

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.