ngClass not updating the class

回眸只為那壹抹淺笑 提交于 2019-12-24 04:10:06

问题


I am using angularJS with bootstrap to design a navigation on my application. I have defined a main controller and inside the main controller I call different page views using the ng-view attribute and then these individual views have their own controllers.

I am using the ng-class attribute to add the active class to my links, however, it is not working. The expression evaluates to true or false but the ng-class does not updates the class="active" on the elements.

On my home page I have the following code -

<section ng-controller="dashBoardCtrl" class="container">
        <section class="row">
            <h1>DME DashBoard | <small>WFM HeadCount</small> </h1>
        </section>

        <section class="row">
            <ul class="nav nav-tabs">
                <li role="presentation" ng-class="{active: {{path=='/'}}}"><a ng-href="#/">Home</a></li>
                <li role="presentation" ng-class="{active: {{path=='/login'}}}"><a ng-href="#/login">Login</a></li>
            </ul>
        </section>

        <section ng-view></section>
    </section>

This is how the routes are configured on the app -

dmeDash.config(['$routeProvider', function ($routeProvider) {
    $routeProvider
        .when('/', {
            templateUrl: '/views/home.html',
            controller: 'homePageCtrl'
        })
        .when('/login', {
            templateUrl: '/views/login.html',
            controller: 'loginCtrl'
        })
        .otherwise({
            redirectTo: '/'
        });
}]);

The dashBoardCtrl have the following code -

dmeDashControllers.controller('dashBoardCtrl', ['$scope','$location', function($scope, $location) {

    $scope.path = $location.$$path;

    $scope.$watch('path',function(n,o) {

    })

}]);

Home Page Controller has the following code -

dmeDashControllers.controller('homePageCtrl', ['$scope','$location', function($scope, $location) {

    $scope.$parent.path = $location.$$path;
}]);

The log in page controller has the following code -

dmeDashControllers.controller('loginCtrl', ['$scope','$location', function($scope, $location) {

    $scope.$parent.path = $location.$$path;

}]);

When I click from Home page to Login page the active class is not removed from home page link and not applied to the login page as well, however, when I view the code in firebug the expressions evaluates to true or false when the page changes.

When I refresh on individual pages the ng-class works correctly but not when using the hyperlinks, please suggest.


回答1:


The syntax is wrong on the template. It should be:

 <li role="presentation" ng-class="{'active': path==='/'}"><a ng-href="#/">Home</a></li>

And as style guide try using the === instead of the simple == because of type coercion. Or test agains truthy or falsy



来源:https://stackoverflow.com/questions/30622998/ngclass-not-updating-the-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!