@{
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>