自定义AngularJS中的services服务

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta http-equiv="X-UA-Compatible"content="IE=edge" />
<meta name="viewport"content="width=device-width,initial-scale=1" />

<title></title>
<meta charset="utf-8" />
<link rel="stylesheet"href="bootstrap-3.3.5/dist/css/bootstrap.min.css" />
</head>
<body data-ng-app="myApp"data-ng-controller="myCtrl">
<div class="container"style="padding:50px">
<div data-ng-model="mesg">
<h2>{{mesg}}</h2>
<p>{{realName}}</p>
<p>{{Http}}</p>
<p>{{data.msg}}</p>
<p>{{name}}</p>
</div>
</div>

<script src="jQuery/jquery-2.1.4.js"></script>
<script src="bootstrap-3.3.5/dist/js/bootstrap.min.js"></script>
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
<script src="js/app.js"></script>
</body>
</html>

app.js

angular.module("myApp", [])
.value("realname", "zhaoliu")
.constant("http", "www.runoob.com")
.factory("Data", function () {
return {
msg: "您好呀!",
setMsg: function () {
this.msg="2016年财色兼收"
}
}
})
.service("User", function () {
this.fName = "melao";
this.lName = "2006";
this.getName = function () {
return this.fName+this.lName
}
})
.controller("myCtrl", function ($scope,realname,http,Data,User) {
$scope.mesg = "Hello AngularJS";
$scope.realName = realname;
$scope.Http = http;
$scope.data = Data;
Data.setMsg();
$scope.name = User.getName();
})

posted @ 2016-01-01 13:12  melao2006  阅读(150)  评论(0编辑  收藏  举报