Angular 1.5.x 组件路由器解析(2)

Posted

技术标签:

【中文标题】Angular 1.5.x 组件路由器解析(2)【英文标题】:Angular 1.5.x Component Router Resolve (2) 【发布时间】:2017-01-14 11:32:08 【问题描述】:

如何让组件路由在执行控制器和渲染视图之前等待数据。类似于ngRouteui-router 中的resolve

文档有这个:

这只是等待url改变,例如:

//Old url
http://localhost/#/

// wait the promise (eg 1s) and then change the url to
http://localhost/#/heroes

但是在点击链接后立即改变了视图,然后改变了url,太奇怪了!

这不会阻止视图渲染或控制器执行(显然)。

这种情况的演示:

代码和预览:plunkr.co(点击英雄)

有一个类似的问题没有答案:Angular 1.5.x Component Router Resolve

【问题讨论】:

【参考方案1】:

我找到了解决办法:

这应该在组件定义中配置:

.component('myComponent', 
    templateUrl: './my-compnent.html',
    controller: MyComponentCtrl,
    $canActivate: function($nextInstruction, $prevInstruction) 
      return Promise.resolve(<true or false>);
    
)

【讨论】:

以上是关于Angular 1.5.x 组件路由器解析(2)的主要内容,如果未能解决你的问题,请参考以下文章