2014-07-09 42 views
0

我有一些项目,我想更新状态时更改,但我不确定如何使用ui-router可以得到此工作。更新和显示状态charis上的状态chage

我已经添加了标题,mainClass,headerSearch到状态(见下文),我想在stateChange成功时访问这些。所以我可能有类似class="{{ $state.curent.title }}"当状态改变了,其将继承title,所以如果家国家是积极它看起来像class="home"

这是怎么了我曾试图设定的事情了(我用的是ng-router在那里我有类似的设置,完美的工作)。

状态控制器:

'use strict'; 

angular.module('cbuiRouterApp') 
    .config(function ($stateProvider) { 
    $stateProvider 
     .state('main', { 
     url: '/', 
     templateUrl: 'app/main/main.html', 
     controller: 'MainCtrl', 
     title: 'Home', 
     mainClass: 'home', 
     headerSearch: null 
     }); 
    }); 

状态更改:

$rootScope.$on('$stateChangeSuccess', function(event, current){ 
    // Assign site Title 
    $rootScope.siteName = 'ChoppingBlock'; 
    //Change Page name 
    $rootScope.title = current.$$route.title; 
    // Add page class to <body> 
    $rootScope.mainClass = current.$$route.mainClass; 
    // add header navigation 
    $rootScope.headerSearch = current.$$route.headerSearch; 
}); 

回答

1

你可以

app.run(['$rootScope', '$state', function ($rootScope, $state){ 
    $rootScope.$state = $state; 
}]); 

,它有可能class="{{ $state.current.title }}"$scope.$state.current.title任何控制器之后。

+0

传说!为此欢呼,就像一个魅力,我实际上已经看到这个https://github.com/angular-ui/ui-router/blob/master/sample/app/app.js,但我认为我必须已经实施它不正常。 – Daimz