AngularJS / ui-router - 通过结果循环状态需要关闭

问题描述 投票:1回答:1

我正在使用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'。我确定这是一个关闭问题,但是我无法确切地了解解决方案是如何工作的(它实际返回的是什么),因此无法弄清楚如何关闭我的state.template范围。

有任何想法吗?谢谢。

javascript angularjs angular-ui-router closures
1个回答
1
投票

好的,我找到了解决方案。

在每个解析中,我创建了另一个调用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));
© www.soinside.com 2019 - 2024. All rights reserved.