angularJS1笔记-(4)-自定义服务

html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<div ng-app="myApp">
    <div ng-controller="firstController">
        <p>{{name}}</p>
        <p>{{serviceMsg1</p>
        <p>{{serviceMsg2}}</p>
    </div>
</div>

<script type="text/javascript" src="../../vendor/angular/angularJs.js"></script>
<script type="text/javascript" src="app/index.js"></script>

</body>
</html>

 js:

var myApp = angular.module('myApp',[],function ($provide) {
   console.log(1);
   //自定义服务
    $provide.provider('CustomService1',function () {
       this.$get = function () {
           return {
               message : '你好 CustomService1 Message'
           }
       }
    });
    $provide.provider('CustomService2',function () {
        //此处的$get用于返回factory的实例
        this.$get = function () {
            return {
                message : '你好 CustomService2 Message'
            }
        }
    });
});
myApp.controller('firstController',function (CustomService1,CustomService2,$scope) {
    $scope.name = '张三';
    $scope.serviceMsg1 = CustomService1.message;
    $scope.serviceMsg2 = CustomService2.message;
});

  结果:

 

posted @ 2017-04-28 16:39  My Way!  阅读(171)  评论(0编辑  收藏  举报