angularjs ui-router设置活动链接不起作用

时间:2014-01-26 20:19:54

标签: angularjs angular-ui-router

我正在尝试设置活动链接 在当前标签上,如

in jade
ul.nav.navbar-nav
                            li(ng-class="{ active: $state.includes('index') }")
                                a(ui-sref='index') Home
                            li(ng-class="{ active: $state.includes('post') }")
                                a(ui-sref='post') Post
 html
<li ng-class="{ active: $state.includes('index') }"><a ui-sref="index" href="#/">Home</a></li>
              <li ng-class="{ active: $state.includes('post') }"><a ui-sref="post" href="#/post">Post</a></li>




js   .config(function($stateProvider,RestangularProvider,PostProvider,PostsProvider,MediaProvider) {
            $stateProvider
                .state('index', {
                    url: '/',
                    templateUrl: 'admin/views/index.html',
                    controller: 'HomeCtrl'
                })
                .state('post', {
                    url: '/post',
                    templateUrl: 'admin/views/post/index.html',
                    resolve: {
                        posts: function(Posts){
                            return Posts.all();
                        }
                    },
                    controller: 'PostIndexCtrl'
                })

但它不起作用。 但如果我试着检查一下:

.controller('PostIndexCtrl', function ($scope,posts,$state) {
        $scope.posts = posts;
        console.log($state.includes('post'));
    })

说实话。

你知道这是什么问题吗?

1 个答案:

答案 0 :(得分:5)

.run(function ($state,$rootScope, $log) {
   $rootScope.$state = $state;
});

如果要访问它,需要在$ rootScope上放置$ state。 非常感谢ui-router伙计们:)