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

html:




    
    Title



{{name}}

{{serviceMsg1

{{serviceMsg2}}

 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;
});

  结果:

angularJS1笔记-(4)-自定义服务_第1张图片

 

转载于:https://www.cnblogs.com/yk123/p/6782226.html

你可能感兴趣的:(angularJS1笔记-(4)-自定义服务)