admin管理员组

文章数量:1332345

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

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

Share Improve this question edited Feb 1, 2014 at 21:24 nymo 5421 gold badge5 silver badges15 bronze badges asked Feb 1, 2014 at 9:16 Filippo orettiFilippo oretti 49.9k96 gold badges229 silver badges351 bronze badges 2
  • As I know, indexOf is not supported in some browsers. If that's the case, you need a polyfill – Khanh TO 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 – Filippo oretti Commented Feb 1, 2014 at 9:21
Add a ment  | 

1 Answer 1

Reset to default 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.

本文标签: javascriptAngular jsngclassquotquot with simple condition won39t workStack Overflow