javascript - AngularJS/ui-router - 循环状态,解析,需要关闭

标签 javascript angularjs angular-ui-router closures

我正在使用 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/

相关文章:

javascript - 使 HTML 输入区域只读但可选择

JavaScript。变量未正常赋值

javascript - 带有ajax形式的谷歌recaptcha

javascript - AngularJS 如何在事件监听器后更新 View /范围

javascript - localForage 使用 WebSQL web IndexedDB 可用

javascript - 具有不同运行时配置的多个 Restangular 服务

javascript - 在 React 中,当我触发另一个组件中的 Add 函数时,如何正确使用钩子(Hook)来更新一个组件?

javascript - 将 jQuery Hover 转换为 Angular

angularjs - Angular 1.5 组件和 ui-router 1.0 : Add attributes to inserted dom element

javascript - 在 Angular ui-router 中是否可以为特定 View 设置动画?