angularJS中的服务

angular.module('app',[])
.value('test', "张华涛")//值是可以改变的
.value('test', "許飛")//值是可以改变的
.constant('http', "www.lulucy.cn")//值是不能改变的
.constant('http', "www.lulucy.com")//值是不能改变的
.factory('Data', [function () {
//这个里面是返回的一个对象,直接return
return {
msg:"你好啊!!!!!",
setMsg:function(){
this.msg = "我非常的不好!!!!";
}
};
}])
.service('User', [function () {
this.firstname = "张华";
this.lastname = "许飞";
this.getName = function(){
return this.firstname + "要打---" + this.lastname
}
}])
.controller('myCtrl', function ($scope,test,http,Data,User) {
$scope.test = test;
$scope.msg = "leo";
$scope.http = http;
$scope.data = Data;
console.log($scope.data);
$scope.data.setMsg();//自执行
$scope.user = User;
console.log($scope.user);
$scope.username = User.getName();
})

原文地址:https://www.cnblogs.com/leo666/p/5701362.html