15

我有一个基于以下示例的菜单:

 <nav  data-ng-controller="menuContrl" class="menuItem">
     <a  data-ng-class='{active:isActive("/{{item.path}}")}' data-ng-repeat="item in menu" href="#/{{item.path}}">
         <span>{{item.title}}</span>
     </a>
 </nav>

item 是一个对象,包含菜单项信息。以下是指令和控制器的 JavaScript 代码:

var app = angular.module("coolApp",[]);

function menuContrl($scope,$location){
    $scope.menu=menu;
    $scope.isActive = function(path){
        return ($location.path()==path)
    } 
}

问题是在页面渲染期间只ng-class设置class一次active,但是当您单击菜单项时,什么也没有发生。我想这是因为菜单本身没有重新加载,我只是更改里面的数据<div>。那么如何在不重新加载整个页面的情况下使其工作呢?

4

3 回答 3

21

试试这个:- http://jsfiddle.net/uDPHL/146/

旧版本存在此问题angular js Reference,升级到angular js 1.2.0版本后问题得到解决。

JS:-

var navList = angular.module('navList', []);    

navList.controller('navCtrl', ['$scope', '$location', function ($scope, $location) {

    $scope.navLinks = [{
        Title: 'home',
        LinkText: 'Home',
    }, {
        Title: 'about',
        LinkText: 'About Us'
    }, {
        Title: 'contact',
        LinkText: 'Contact Us'
    }];

    $scope.navClass = function (page) {
        var currentRoute = $location.path().substring(1) || 'home';
        return page === currentRoute ? 'active' : '';
    };   

}]);

HTML:-

<div class="well sidebar-nav" ng-app="navList">
    <ul class="nav nav-list" ng-controller="navCtrl">
        <li ng-repeat="navLink in navLinks" ng-class="navClass('{{navLink.Title}}')"> <a href='#/{{navLink.Title}}'>{{navLink.LinkText}}</a> 
        </li>
    </ul>
</div>
于 2013-11-13T07:58:49.943 回答
16

我发现如果你去的话会容易一些:

<li ng-repeat="i in mainMenu" ng-class="{'active':i.path == path}">

然后你可以改变控制器中的 $scope.path 。

于 2013-12-09T22:40:40.857 回答
0

//app.run
App.run(["$rootScope", "$state", "$stateParams",function ($rootScope, $state, $stateParams) {
        // Set reference to access them from any scope
        $rootScope.$route = $state;
}]);
        

// app.config
        $stateProvider
            .state('admin', {
                url: '/admin/',
                templateUrl: 'app/admin/admin.html',
                controller: 'AdminController',
            })
            .state('admin.dashboard', {
                url: 'dashboard',
                title: 'Dashboard',
                activetab: 'dashboard',
                controller: 'DashboardController',
                templateUrl: 'app/admin/dashboard.html',

            })
            .state('admin.view1', {
                url: 'view1',
                title: 'view1',
                activetab: 'view1',
                controller: 'view1Controller',
                templateUrl: 'app/admin/view1.html',
               
            })
            .state('admin.view2', {
                url: 'view2',
                title: 'view2',
                activetab: 'view2',
                controller: 'View2Controller',
                templateUrl: 'app/admin/view2.html',
            });
<li class="h4" ng-class="{'active': $route.current.activetab == 'dashboard'}">
                        <a>Dashboard</a>
                    </li>
                    <li class="h4" ng-class="{'active': $route.current.activetab == 'view1'}">
                        <a>view1</a>
                    </li>
                    <li class="h4" ng-class="{'active': $route.current.activetab == 'view2'}">
                        <a active-link="active">view2</a>
                    </li>

于 2017-10-12T05:54:17.380 回答