2

Hi, i'm trying to add a class to an element via ng-class. As you can see, the code is very simple but it doesn't add the class:

app.run(function($rootScope, $location,$http,ngDialog,array_helper){
    var update =  function() {
        /*CONFIG PARAMS*/
        $rootScope.config = {};
        $rootScope.config.app_routes = [
            "/",
            "/channels",
            "/channels/create",
            "/users",
            "/auth/login",
            "/auth/signup"
        ];
        console.log($rootScope.config.app_url);
        console.log($rootScope.config.app_routes.indexOf($rootScope.config.app_url));
    };
    $rootScope.$on('$routeChangeStart', function() { 
        update(); 
    });
});

Then in the view, i do:

<div ng-view ng-animate class="" ng-class="{'slide': config.app_routes.indexOf(config.app_url) == -1}"></div>

But it seems it never works, as it never adds the class slide. Meanwhile console.log works great and returns -1 only when it is right :O

2
  • As I know, indexOf is not supported in some browsers. If that's the case, you need a polyfill Commented Feb 1, 2014 at 9:21
  • @KhanhTO it won't work only where it is supported man :d on Chrome it doesn't works Commented Feb 1, 2014 at 9:21

1 Answer 1

8

Move your logic into a scope variable:

<div ng-view ng-animate ng-class="{ 'slide': someVar }"></div>

and set $scope.someVar = true whenever config.app_routes.indexOf(config.app_url) == -1. How you do this depends how and when config.app_routes is updated.

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

1 Comment

I had the same issue using a variable inside controller's vm. Moving the variable to $scope solved the problem.

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.