AngularJS中service,factory,provider的区别

1、service引导

刚开始学习Angular的时候,常常被误解和被初学者问到的组件是 service(), factory(), 和 provide()这几个方法之间的差异。This is where we'll start the twenty-five days of Angular calendar.java

2、service

在Angular里面,services做为单例对象在须要到的时候被建立,只有在应用生命周期结束的时候(关闭浏览器)才会被清除。而controllers在不须要的时候就会被销毁了。git

这就是为何使用controllers在应用里面传递数据不可靠的缘由,特别是使用routing的时候。Services are designed to be the glue between controllers, the minions of data, the slaves of functionality, the worker-bees of our application(就是说services在应用的controllers、 方法、数据以前起到了很关键的做用)angularjs


如今咱们开始看怎么建立service。每一个方法咱们都会看到下面两个同样的参数:github

  • name-咱们要定义的service的名字后端

  • function-service方法api

他们都建立了相同的底层对象类型。实例化后,他们都建立了一个service,这些对象没有什么功能上的差异。浏览器

一、factory()

Angular里面建立service最简单的方式是使用factory()方法。app

factory()让咱们经过返回一个包含service方法和数据的对象来定义一个service。在service方法里面咱们能够注入services,好比 $http 和 $q等。ide

angular.module('myApp.services')
.factory('User', function($http) { // injectables go here
  var backendUrl = "http://localhost:3000";  var service = {    // our factory definition
    user: {},
    setName: function(newName) { 
      service.user['name'] = newName; 
    },
    setEmail: function(newEmail) {
      service.user['email'] = newEmail;
    },
    save: function() {
      return $http.post(backendUrl + '/users', {
        user: service.user
      });
    }
  };  return service;
});


  • 在应用里面使用factory()方法函数

在应用里面能够很容易地使用factory ,须要到的时候简单地注入就能够了

angular.module('myApp')
.controller('MainCtrl', function($scope, User) {
  $scope.saveUser = User.save;
});


  • 何时使用factory()方法

在service里面当咱们仅仅须要的是一个方法和数据的集合且不须要处理复杂的逻辑的时候,factory()是一个很是不错的选择。

注意:须要使用.config()来配置service的时候不能使用factory()方法

二、service()

service()经过构造函数的方式让咱们建立service,咱们可使用原型模式替代javaScript原始的对象来定义service。

和factory()方法同样咱们也能够在函数的定义里面看到服务的注入

angular.module('myApp.services')
.service('User', function($http) { // injectables go here
  var self = this; // Save reference
  this.user = {};
  this.backendUrl = "http://localhost:3000";
  this.setName = function(newName) {
    self.user['name'] = newName;
  }
  this.setEmail = function(newEmail) {
    self.user['email'] = newEmail;
  }
  this.save = function() {
    return $http.post(self.backendUrl + '/users', {
      user: self.user
    })
  }
});


这里的功能和使用factory()方法的方式同样,service()方法会持有构造函数建立的对象。

  • 在应用里面使用service()方法

angular.module('myApp')
.controller('MainCtrl', function($scope, User) {
  $scope.saveUser = User.save;
});


  • 何时适合使用service()方法

service()方法很适合使用在功能控制比较多的service里面

注意:须要使用.config()来配置service的时候不能使用service()方法

三、provider()

provider()是建立service最底层的方式,这也是惟一一个可使用.config()方法配置建立service的方法

不像上面提到的方法那样,咱们在定义的this.$get()方法里面进行依赖注入

angular.module('myApp.services')
.provider('User', function() {
  this.backendUrl = "http://localhost:3000";
  this.setBackendUrl = function(newUrl) {
    if (url) this.backendUrl = newUrl;
  }
  this.$get = function($http) { // injectables go here
    var self = this;
    var service = {
      user: {},
      setName: function(newName) {
        service.user['name'] = newName;
      },
      setEmail: function(newEmail) {
        service.user['email'] = newEmail;
      },
      save: function() {
        return $http.post(self.backendUrl + '/users', {
          user: service.user
        })
      }
    };
    return service;
  }
});


  • 在应用里面使用provider()方法

为了给service进行配置,咱们能够将provider注入到.config()方法里面

angular.module('myApp')
.config(function(UserProvider) {
  UserProvider.setBackendUrl("http://myApiBackend.com/api");
})


这样咱们就能够和其余方式同样在应用里面使用这个service了

angular.module('myApp')
.controller('MainCtrl', function($scope, User) {
  $scope.saveUser = User.save;
});


  • 何时使用provider()方法

  1. 当咱们但愿在应用开始前对service进行配置的时候就须要使用到provider()。好比,咱们须要配置services在不一样的部署环境里面(开发,演示,生产)使用不一样的后端处理的时候就可使用到了

  2. 当咱们打算发布开源provider()也是首选建立service的方法,这样就可使用配置的方式来配置services而不是将配置数据硬编码写到代码里面。

还能够看看这篇翻译:http://www.oschina.net/translate/top-10-mistakes-angularjs-developers-make


点击查看完整的代码:https://gist.github.com/auser/7743235

相关文章
相关标签/搜索