我也谈“the difference between Factory, Service, and Provider in Angular”
2015-04-23 15:35
666 查看
看完这篇文章之后的理解与实践:原文地址: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("\n")); // 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("\n")); 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>
相关文章推荐
- What is the difference between provider network and self-service network in OpenStack?
- What’s the Difference Between a Value Provider and Model Binder?
- The Difference Between @Helpers and @Functions In WebMatrix
- what is the difference between "isempty"and "isnull"in the qt bool QString::isEmpty () const Retur.
- what is the difference between static and normal variables in c++
- angularJs-The factory and service of angularJs
- macro与inline的区别 What is the difference between macro and inline?
- What is the difference between Views and Materialized Views in Oracle?
- What's the difference between @Component, @Repository & @Service annotations in Spring?
- Understand the difference between IN and EXISTS in subqueries
- Difference between component and directive in Angular 2
- What is the difference between SVD and matrix factorization in context of recommendation engine?
- The difference between the 'Local System' account and the 'Network Service' account?
- What’s the difference between an interface and an abstract class in Java?
- The Difference Between “Add Web Reference” and “Add Service Reference”
- the difference between final and static in java
- The Difference Between “Add Web Reference” and “Add Service Reference”
- In PHP5, what is the difference between using self and $this? When is each appropriate?
- whats is the difference between train, validation and test set, in neural networks?
- The difference between BEB in MACA and in MACAW