从装饰器设置视图的名称 - Angular Ui Router [英] Setting view's name from decorator - Angular Ui Router
本文介绍了从装饰器设置视图的名称 - Angular Ui Router的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我以这种方式定义我的状态:
I'm defining my states in this way:
var parentStates = [
{state : 'home', url: '/home', template: 'home.html'},
{state : 'about', url: '/about', template: 'about.html'},
{state : 'contact', url: '/contact', template: 'contact.html'},
{state : 'home.data', url: '', template: 'data.html'},
{state : 'about.data', url: '', template: 'data.html'},
{state : 'contact.data', url: '', template: 'data.html'}
];
$urlRouterProvider.otherwise("/main/home");
$stateProvider
.state("main", { abtract: true, url:"/main",
views: {
"viewA": {
templateUrl:"main.html"
}
}
});
parentStates.forEach(function(value){
$stateProvider
.state("main." + value.state, {
url: value.url,
views: {
"": {
templateUrl: value.template
}
},
})
});
我想写一个 'decorator'
用于根据 'templateUrl'
设置视图的名称 (如上所示,视图的名称为空).
I want to write a 'decorator'
for setting the view's name based on 'templateUrl'
(As you see above, the view's name is empty).
这是装饰器的代码:
$stateProvider.decorator('views', function (state, parent) {
var result = {},
views = parent(state);
// Don't touch the 'main state'
if (state.name === "main") {
return views;
}
angular.forEach(views, function (config, name) {
if(config.templateUrl=='data.html'){
result[name] = 'viewC@main';
}
else{
result[name] = 'viewB@main';
}
});
return result;
});
当然,这行不通.我有点失落.
Of course, this doesn't work. I'm a little bit lost.
推荐答案
你快到了.让我们稍微简化一下状态定义(因为我们不需要嵌套的视图对象,我们稍后会创建它):
You are almost there. Let's simplify a bit the state definition (because we do not need nested views object, we will create it later):
parentStates.forEach(function(value) {
$stateProvider
.state("main." + value.state, {
url: value.url,
templateUrl: value.template,
})
});
这将是装饰器:
$stateProvider.decorator('views', function(state, parent) {
var result = {},
views = parent(state);
// some example when to not inject resolve
if (state.name === "main") {
return views;
}
angular.forEach(views, function(config, name) {
// the super child template
if(config.templateUrl === 'data.html'){
result['viewC@main'] = config;
}
else{
result['viewB@main'] = config;
}
});
return result;
});
检查它这里
还要注意这些:
这篇关于从装饰器设置视图的名称 - Angular Ui Router的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文