简体   繁体   中英

Why isn't ng-class working?

I want to highlight currently selected menu item with AngularJS. I have this HTML:

<li ng-repeat="m in menuLinks">
    <a ng-class="{active : isActive('{{m.url}}')}" href="{{m.url}}" active-link="active" onclick="closeMenu()">{{m.title}}</a>
</li>

And this in the controller:

$scope.isActive = function (viewLocation) {
        alert(viewLocation);
        if ($location.path().indexOf(viewLocation) === 0) {
            return true;
        } else {
            return false;
        }
};

The function isActive() gets called because alerts show up, but when I inspect the elements I get this:

<a class="ng-binding" ng-class="{active : isActive('/#/sessions')}" href="/#/sessions" active-link="active" onclick="closeMenu()">Sessions</a>

What am I missing here?

You don't need the expression in ng-class , just use it like this:

 <a ng-class="{active : isActive(m.url)}" ng-href="{{m.url}}" active-link="active" ng-click="closeMenu()">{{m.title}}</a>

and I recommend using ng-href instead of just href and ng-click instead of onclick ;)

$ location.path()不返回数组,而indexOf搜索项目在数组中的索引。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM