码迷,mamicode.com
首页 > Web开发 > 详细

AngularJs练习Demo15自定义服务

时间:2016-06-22 00:20:55      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

 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自定义服务

标签:

原文地址:http://www.cnblogs.com/sumg/p/5605372.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!