我正在使用 ui-router 在我们的 AngularJS 应用程序中动态构建我的状态。
给定一组这样的状态(例如):
const dynamicStates = [
{name: 'alpha', template: '123'},
{name: 'bravo', template: '234'}
];
然后像这样构建状态(例如):
const states = [];
dynamicStates.forEach(state =>
states.push(
state: state.name,
resolve: {
templateDetails: ['Service', (Service) =>
Service.getTemplate(state.template)
.then(response => response)
]
}
)
})
states.forEach(states => $stateProvider.state(state));
state.template,在 resolve 函数中总是'123'。我确定这是一个关闭问题,但我无法确切地了解 resolve 是如何工作的(它实际返回了什么),因此无法弄清楚如何关闭我的 state.template 周围的范围.
有什么想法吗?谢谢。
最佳答案
好的,我找到了解决方案。
在每次解析中,我都创建了另一个调用 closureAroundTemplateId 的函数,并返回模板 ID 值。然后将其传递给下一个解析函数,该函数实际获取模板详细信息。
我这样做了(如上所述,示例代码):
function closureAroundTemplateId (type) {
return function(){
return type['template'];
}
}
const states = [];
dynamicStates.forEach(state =>
states.push(
state: state.name,
resolve: {
templateId: closureAroundFormTemplateId(state),
templateDetails: ['Service', 'templateId', (Service, templateId) =>
Service.getTemplate(templateId)
.then(response => response)
]
)
})
states.forEach(states => $stateProvider.state(state));
关于javascript - AngularJS/ui-router - 循环状态,解析,需要关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48948700/