zoukankan      html  css  js  c++  java
  • 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;
    });
    

      结果:

  • 相关阅读:
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    lambda表达式
    VIM--保存和退出等命令
  • 原文地址:https://www.cnblogs.com/yk123/p/6782226.html
Copyright © 2011-2022 走看看