AngularJs练习Demo15自定义服务

Posted 编程猴子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AngularJs练习Demo15自定义服务相关的知识,希望对你有一定的参考价值。

 1 @{
 2     Layout = null;
 3 }
 4 
 5 <!DOCTYPE html>
 6 
 7 <html>
 8 <head>
 9     <meta name="viewport" content="width=device-width" />
10     <title>Provider</title>
11     <script type="text/javascript" src="~/Scripts/angular.js"></script>
12 </head>
13 <body>
14     <div ng-app="myApp">
15         <div ng-controller="firstController">
16             {{name}}
17         </div>
18     </div>
19     <script type="text/javascript">
20         var myApp = angular.module("myApp", []);
21         myApp.config(function ($provide) {
22             $provide.provider(‘providerServices01‘, function () {//自定义服务,通过config来自定义服务
23                 this.$get = function () {
24                     return {
25                         message: ‘this is providerServices01‘
26                     }
27                 }
28             });
29 
30             $provide.provider(‘providerServices02‘, function () {//自定义服务,通过config来自定义服务
31                 this.$get = function () {
32                     var _name = "";
33                     var service = {};
34                     service.setName = function (name) {
35                         _name = name;
36                     }
37                     service.getName = function (name) {
38                         return _name;
39                     }
40                     return service;
41                 }
42             });
43 
44         });
45         myApp.controller("firstController", ["$scope", "providerServices01", "providerServices02", function ($scope, providerServices01, providerServices02) {
46             providerServices02.setName("李四");
47             $scope.name = providerServices02.getName();
48         }]);
49        
50 
51     </script>
52 
53 </body>
54 </html>

 

以上是关于AngularJs练习Demo15自定义服务的主要内容,如果未能解决你的问题,请参考以下文章

AngularJs练习Demo8

AngularJs练习Demo7

angularjs自定义指令实现分页插件

AngularJs练习Demo3

AngularJs练习Demo9

AngularJs练习Demo6