$routeProvider resolve
parameter can allow for injecting additional dependencies to the controller function. How to combine it with explicit dependecy injection declaration?
Ecample:
angular.module('myModule', [])
.config(function($routeProvider) {
$routeProvider.
when('/tabOne', { templateUrl : 'tabOne.html', controller: TabOne,
resolve: {
someDependency: SomeDependency.factoryFunction
}
});
});
And then:
TabOne.$inject = [ '$scope', 'someFirstService', 'someOtherService' ];
As you can see DI declaration above will inject two services to my TabOne
controller (someFirstService
and someOtherService
). Those two work fine, but I want my route to change only after someDependency
has been resolved and injected to TabOne
as well. If I simply add someDependency
to Controller function's arguments list it will fail with DI error.
Any ideas how to do that?
First, does your controller just need to wait until the dependency is resolved prior to being invoked, or does it actually need the resolved value of SomeDependency.factoryFunction ?
It seems as if the case is the latter: you want the resolve value.
In this case, one approach that would work would be to have two TabOne controllers.
angular.module('myModule', [])
.config(function($routeProvider) {
$routeProvider.
when('/tabOne', { templateUrl : 'tabOne.html', controller: TabOneRouted,
resolve: {
someDependency: SomeDependency.factoryFunction
}
});
});
function TabOne($scope, someFirstService, someOtherService) {
... code here ...
}
function TabOneRouted($scope, someFirstService, someOtherService, someDependency) {
TabOne($scope, someFirstService, someOtherService);
... your code dealing with someDependency here ...
}