zoukankan      html  css  js  c++  java
  • 我也谈“the difference between Factory, Service, and Provider in Angular”

    看完这篇文章之后的理解与实践:原文地址:http://tylermcginnis.com/angularjs-factory-vs-service-vs-provider/

    <!doctype html>
    <html ng-app="myModule">
    <head>
        <script src="../lib/js/angular.min.js"></script>
    </head>
    <body>
    <script>
        var myModule = angular.module('myModule', []);
        myModule.factory('greeter', function($window){
            return {
                greet: function(text){
                    $window.alert(text);
                }
            };
        });
    
        function myController($scope,greeter,notify){
            $scope.sayHello = function(){
                greeter.greet('Hello, world!');
            }
            $scope.callNotify = function(msg){
                notify.say(msg);
            };
    //        scope.callNotify = function(msg){
    //            notify(msg);
    //        };
        }
    
    //    myModule.factory('notify', function($window){
    //       var msgs = [];
    //        return function(msg){
    //            msgs.push(msg);
    //            if(msgs.length==3){
    //                console.info(msgs);
    //                $window.alert(msgs.join("
    "));
    //                msgs = [];
    //            }
    //        }
    //    });
        myModule.service('notify', function($window){
           var msgs = [];
            return this.say = function(msg){
                msgs.push(msg);
                if(msgs.length==3){
                    console.info(msgs);
                    $window.alert(msgs.join("
    "));
                    msgs = [];
                }
            }
        });
    </script>
    <div ng-controller="myController">
        <button ng-click="sayHello()">Hello</button>
        <input type="text" ng-model="message" />
        <button ng-click ="callNotify({{'message'}})">Notify</button>
    </div>
    </body>
    </html>
    

      

  • 相关阅读:
    DHCP DHCPv6
    DHCPv6协议
    IPv6邻居发现协议
    CentOS下禁止防火墙
    centOS下更新yum源
    centOS下yum报错
    Flink+Kafka整合的实例
    Flink基本概念
    Ubuntu16.04下配置ssh免密登录
    Zookeeper+Kafka的单节点配置
  • 原文地址:https://www.cnblogs.com/oxspirt/p/4450707.html
Copyright © 2011-2022 走看看