I actually have three issues with $canActivate (also look at code below to better understand the issues):
- When clicking on
<a ng-link="['MachineDetails', { id: 'c6bd76947fc0' }]</a>
I cannot access the id from $canActivate (url and route have not chnaged yet so can't get it either through an inject $location or injected $rootRouter) - I cannot figure out a way to pass data from $canActivate to the controller.
I cannot reroute from $canActivate (by using $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; }
The above code is my component definition written in ES6.