AngularJs练习Demo15自定义服务

 @{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Provider</title>
<script type="text/javascript" src="~/Scripts/angular.js"></script>
</head>
<body>
<div ng-app="myApp">
<div ng-controller="firstController">
{{name}}
</div>
</div>
<script type="text/javascript">
var myApp = angular.module("myApp", []);
myApp.config(function ($provide) {
$provide.provider('providerServices01', function () {//自定义服务,通过config来自定义服务
this.$get = function () {
return {
message: 'this is providerServices01'
}
}
}); $provide.provider('providerServices02', function () {//自定义服务,通过config来自定义服务
this.$get = function () {
var _name = "";
var service = {};
service.setName = function (name) {
_name = name;
}
service.getName = function (name) {
return _name;
}
return service;
}
}); });
myApp.controller("firstController", ["$scope", "providerServices01", "providerServices02", function ($scope, providerServices01, providerServices02) {
providerServices02.setName("李四");
$scope.name = providerServices02.getName();
}]); </script> </body>
</html>
上一篇:linux php命令安装


下一篇:C++ - 复制(copy) 和 虚复制(virtual copy) 的 区别