标签:
var myApp = angular.module("myApp",[]);
// controller 中引用 provider factory service 的时候,不需要添加后缀。。。
myApp.controller("myController",function($scope,my,myFactory,myService){
$scope.hellos = [
my.sayHello(),
myFactory.sayHello(),
myService.sayHello()
]
});
myApp.service("myService",function(){
// 注意this 对象
this.sayHello = function () {
return "hello world for service";
}
});
// 注意 注意使用$get 方法
myApp.provider("my", function () {
this.name = "default";
this.$get = function () {
var name = this.name;
return {
sayHello: function () {
return "hello " + name+ " for provider" ;
}
}
}
this.setName = function(name){
this.name = name;
}
});
// 注意 return
// var xxx = {} ;
// return xxx;
myApp.factory("myFactory", function () {
return{
sayHello: function () {
return "hello world for factory";
}
}
});
// 对于 provider 必须添加 "provider"后缀
myApp.config(function (myProvider) {
myProvider.setName("world");
})
angluar 区分service/factory/provider 的“hello world”版
标签:
原文地址:http://my.oschina.net/bosscheng/blog/406974