Angularjs ui路由器视图接收变量
目前正在了解ui路由器。我有这个:Angularjs ui路由器视图接收变量,angularjs,angular-ui-router,Angularjs,Angular Ui Router,目前正在了解ui路由器。我有这个: $stateProvider .state('taxonomy', { url: '/lista/:taxonomy', views: { '' : { templateUrl: '/js/app/taxonomy/index.tpl.html', controller: 'taxonomy.index.controller', controllerAs:
$stateProvider
.state('taxonomy', {
url: '/lista/:taxonomy',
views: {
'' : {
templateUrl: '/js/app/taxonomy/index.tpl.html',
controller: 'taxonomy.index.controller',
controllerAs: 'vm',
myClass: 'classForThisOne'
},
'sidebarRight@': {
templateUrl: '/js/app/taxonomy/sidebarRight.tpl.html',
/* no controller = no variable */
}
}
})
.state('taxonomy.detail', {
url: '/:mode/:taxonomyId?',
views: {
'sidebarRight@' : {
templateUrl: '/js/app/taxonomy/detail.tpl.html',
controller: 'taxonomy.detail.controller',
controllerAs: 'vm',
myClass: 'myClassForThisController'
},
'': {
myClass: 'myClassForController taxonomy.index.controller'
}
}
});
我希望能够做到的是能够在我使用controller
和controllerAs
变量指定的控制器中使用“myClass”属性。这能做到吗
我试图观看
$stateChangeStart
事件和$stateProvider.decorator('views',function(state,parent){})
,但运气不佳。您可以像这样传递自定义数据:
views: {
'' : {
templateUrl: '/js/app/taxonomy/index.tpl.html',
controller: 'taxonomy.index.controller',
controllerAs: 'vm',
// the 'data' property can be used for custom data
data: {
myClass: 'classForThisOne'
}
},
然后在控制器中,注入$state
,然后检索数据:
function myCtrl($state){
var myClass = $state.current.data.myClass;
}
请参阅以供参考:
请注意,以上是最佳实践(使用“数据”对象),但您也应该能够像这样做:
views: {
'' : {
templateUrl: '/js/app/taxonomy/index.tpl.html',
controller: 'taxonomy.index.controller',
controllerAs: 'vm',
myClass: 'classForThisOne'
},
和检索:
var myClass = $state.current.myClass;
我没试过这个,但这个应该有用。在控制器中写入以下内容:
$scope.getClass = function() {
var currentState = $state.current;
// Or use any other named view instead of "sidebarRight@"
return currentState.views['sidebarRight@'].myClass;
};
你能提供一个弹弓或小提琴吗?大致上。但我不知道控制器中视图的名称,是吗。因此我需要硬编码“sidebarRight@”,我不需要它。Nah-$state.current.data和$state.current.myClass给我空值(如未定义的值)。