AngularJs Facroty Service Provider 不同

AngularJs Facroty Service Provider 不同

 

HTML

   1: <div  ng-app='MyApp'  data-ng-controller="MainCtrl">
   2:       <div>
   3:          Factory: {{FactoryName}}
   4:       </div>
   5:       <div>
   6:          Service: {{ServiceName}}
   7:       </div>
   8:       <div>
   9:          Provider: {{ProviderName}}
  10:       </div>
  11:   </div>

SCRIPT

   1: var app = angular.module('MyApp', []);
   2:  
   3: function build() {             //Service
   4:     this.name = "Factory";
   5:     this.age = 10;
   6:     this.$get = function () {  //Provider
   7:         var self = this;
   8:         self.name = "Provider";
   9:         self.age = 90;
  10:         return self;
  11:     }
  12:  
  13:     return this.name;          //Factory
  14: }
  15:  
  16: app.factory('ShowFactory', build);
  17: app.service('ShowService', build);
  18: app.provider('ShowProvider', build);
  19:  
  20: function MainCtrl($scope, ShowService, ShowFactory, ShowProvider) {
  21:     $scope.FactoryName = ShowFactory; // 
  22:     $scope.ServiceName = ShowService;  // create new
  23:     $scope.ProviderName = ShowProvider; // pass by $get
  24: }

RESULT

   1: Factory: Factory
   2: Service: {"name":"Factory","age":10}
   3: Provider: {"name":"Provider","age":90}