Two routes in my application share a controller, but one route requires data to be resolved before the view loads while the other does not.
Here is an example of the routing segments:
...
when('/users', {
controller: 'UsersCtrl',
templateUrl: '/partials/users/view.html',
resolve: {
resolvedData : ['Accounts', function(Accounts) {
return Accounts.get();
}]
}
}).
when('/users/add', {
controller: 'UsersCtrl',
templateUrl: '/partials/users/add.html'
})
...
And here is an example of the controller:
app.controller('UsersCtrl', ['$scope', 'Helper', 'resolvedData',
function($scope, Helper, resolvedData) {
// This works for the first route, but fails for the second route with
// an unknown "resolvedDataProvider" error
console.log(resolvedData);
}]);
Is there a way I can access the resolvedData
in the controller without explicitly using the resolve name as a dependency? So that I can perform a check?
Using the $injector doesn't seem to work. I would like to do something like this:
if ($injector.has('resolveData')) {
var resolveData = $injector.get('resolveData');
}
However, even this approach doesn't work for the route that has the resolveData
set ('/users'):
app.controller('UsersCtrl', ['$scope', 'Helper', '$injector',
function($scope, Helper, $injector) {
// This does not work, it fails with an unknown "resolvedDataProvider" error as well
$injector.get('resolvedData');
}]);
Is there a way to achieve this in angularjs, or should I just create a new controller?
Thank you.