我们先通过一段代码来认识一下
factory
、service
、provider
var myApp = angular.module('myApp', []);
/*
* Factory
*/
myApp.factory('Factory', function() {
return {
sayHello: function() {
return "Hello, 张先生";
}
};
});
/*
* Service
*/
myApp.service('Service', function() {
this.sayHello = function() {
return "Hello, 张先生";
};
});
/*
* Provider
*/
myApp.provider('Provider', function() {
this.name = '先生';
this.$get = function() {
var name = this.name;
return {
sayHello: function() {
return "Hello, " + name;
}
}
};
this.setName = function(name) {
this.name = name;
};
});
/*
* config provider
*/
myApp.config(function(myProvider){
myProvider.setName('张先生');
});
/*
* 方法调用
*/
function MyCtrl($scope, Provider, Factory, Service) {
$scope.hellos = [
Provider.sayHello(),
Factory.sayHello(),
Service.sayHello()];
}