Whisher
Whisher

Reputation: 32716

angularjs ui-router set active link doesn't work

I'm trying to set an active link on the current tab like

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'
                })

but it doesn't work. But if I try to check it:

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

give me true.

Do you know what's the problem ?

Upvotes: 0

Views: 2657

Answers (1)

Whisher
Whisher

Reputation: 32716

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

You need to put $state on $rootScope if you want to access it. A great thanks to the ui-router folks :)

Upvotes: 5

Related Questions