• 我也谈“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>
    

      

  • 相关阅读:
    WEEK
    更新yum源
    Centos6.9安装Mysql5.7.18
    gitlab使用
    gitlab安装
    git客户端
    服务器端配置
    错误问题
    服务器端
    01找出数组中重复的数
  • 原文地址:https://www.cnblogs.com/oxspirt/p/4450707.html
Copyright © 2020-2023  润新知