0

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 ?

3
  • Is $state available on $scope in the place you're doing that? Commented Jan 27, 2014 at 11:01
  • I've thought of the same thing (but it's quite odd because best practice say the scripts go close to </body> tag) my deploy gist.github.com/whisher/8646820 Commented Jan 27, 2014 at 11:17
  • but all in all in MainCtrl I've got the reference to $state Commented Jan 27, 2014 at 11:30

1 Answer 1

5
.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 :)

Sign up to request clarification or add additional context in comments.

1 Comment

Does $state.transitionTo('index'); contribute to the solution? If not, perhaps edit it out to avoid confusion?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.