Hi Derrick nice to see to trying good stuffs
First of all in your example you where trying add to factory method FetchFileList in resolve, thats wrong .. i can give two reasons for it
1) to call any factory method directly , u need to first inject it
2) U cant inject a factory or Service into Config in angularjs
Now let me try to put some basic example to explain what u can do
var app = angular.module("demoApp",[]);
app.provider('loadData', function () {
this.$get = function() {
return {
sayHello: function() {
console.log("Called in resolve");
}
}
};
this.hello = function(){
console.log("Function Called From Resolve");
}
});
app.config(function($routeProvider, loadDataProvider) {
$routeProvider
.when("/user/:name/:city",{
templateUrl : "tmpl_one.html",
controller : "oneCtrl",
resolve : {
loadData : loadDataProvider.hello, //Method defined in Provider
moreData : moreData //Normal Function
someData : function (){} // its also allowed and fine
}
})
.otherwise({
redirectTo : "/user/:name/:city"
})
});
var moreData = function($q,$timeout){
var defer = $q.defer();
$timeout(function(){
defer.resolve("moreData");
}, 2000);
return defer.promise;
};
The part inside $get is the same that you use in factory or service , and this part is not accessible in config, only the member defined outside it like function hello, This is actually availble in config so that before Services are injected into controllers or other services u have the option Configure the Provider may be based on environment or other properties
Like
myApp.provider('helloWorld', function() {
this.$get = function() {
return {
sayHello: function() {
return "Hello, " + name + "!"
}
}
};
this.setName = function(name) {
};
});
//hey, we can configure a provider!
myApp.config(function(helloWorldProvider){
helloWorldProvider.setName('World');
});
So Either define it as line function , normal function or call from Provider , u cant use Factory or Service in config , u use it in .run() though thats executed after config not worth to think of it in this case
I hope u understood the stuffs and why services should not be allowed to be injected and used in config .
Thanks,
Vineeth