【发布时间】:2016-06-01 14:15:57
【问题描述】:
我实际上在使用 $canActivate 时遇到了三个问题(也请查看下面的代码以更好地理解这些问题):
- 当点击
<a ng-link="['MachineDetails', { id: 'c6bd76947fc0' }]</a>时,我无法从 $canActivate (url 并且路线还没有改变,所以也无法通过 注入 $location 或注入 $rootRouter) - 我无法找到将数据从 $canActivate 传递到 控制器。
-
我无法从 $canActivate 重新路由(通过使用 $rootRouter.navigate())。
import template from './machine-details.html'; export const machineDetailsComponent = { template: template, controller: machineDetailsController, $canActivate: canActivate, }; /*@ngInject*/ function machineDetailsController (machineDetailsService) { this.$routerOnActivate = function (nextRoute) { // do something; }; } /*@ngInject*/ function canActivate ($q, $rootRouter, machineDetailsService) { const deferred = $q.defer(); /** * ISSUE 1: * if I get here via ng-link, then $rootRouter.lastNavigationAttempt * shows the route I was on when clicking, however if I type the url manually * (or refresh) I get something I can use which is for example '/machineDetails/c6bd76947fc01' **/ const machineId = $rootRouter.lastNavigationAttempt.split('/')[2]; machineDetailsService.getMachineDetailsData(machineId) .then(successHandler, errorHandler); /** * ISSUE 2: * 'result' does not make its way into controller so I need to call * machineDetailsService.getMachineDetailsData again in controller... **/ function successHandler (result) { deferred.resolve(); // passing on result has no affect } function errorHandler (error) { deferred.resolve(error); /** * ISSUE 3: * $rootRouter.navigate doesn't work here (and controller is * never called) so how do I cause system to go back to list view ? **/ // $rootRouter.navigate(['ListView']); } return deferred.promise; }以上代码是我用 ES6 编写的组件定义。
【问题讨论】:
标签: angularjs angular-component-router