ngClass 不更新类 [英] ngClass not updating the class

查看:21
本文介绍了ngClass 不更新类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 angularJS 和 bootstrap 来设计我的应用程序的导航.我定义了一个主控制器,在主控制器内部我使用 ng-view 属性调用不同的页面视图,然后这些单独的视图有自己的控制器.

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.

我正在使用 ng-class 属性将活动类添加到我的链接,但是,它不起作用.该表达式的计算结果为 true 或 false,但 ng-class 不会更新元素上的 class="active".

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: '/'
        });
}]);

dashBoardCtrl 有以下代码 -

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;

}]);

当我从主页单击到登录页面时,活动类不会从主页链接中删除,也不会应用于登录页面,但是,当我在 firebug 中查看代码时,当页面变化.

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.

当我刷新单个页面时,ng-class 可以正常工作,但在使用超链接时却无法正常工作,请提出建议.

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

推荐答案

模板上的语法错误.应该是:

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

这篇关于ngClass 不更新类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆