【问题标题】:Dependency Injection into Angular Provider依赖注入到 Angular 提供程序
【发布时间】:2014-11-07 06:51:48
【问题描述】:

我想知道是否有任何更清洁的方式注入提供程序。正如我现在所做的那样,我必须有 http = null,然后在 $get 中设置 http = $http,以便我能够在我的函数中使用它。以下是我的提供者代码

CoffeeScript:

do ->
  githubProvider =() ->
    http = null
    getUser =(username) ->
      return  http.get("https://api.github.com/users/" + username)
                  .then (response)->
                    return response.data

    getRepos =(user) ->
      return http.get(user.repos_url)
                  .then (response)->
                    return response.data

    getRepoDetails =(username, repoName) ->
      return http.get("https://api.github.com/repos/" + username + "/" + repoName)
                  .then (response)->
                    return response.data

    getRepoCollaborators =(repo) ->
      return http.get(repo.contributors_url)
            .then (response)->
              return response.data

    this.$get =["$http", ($http) ->
      http = $http
      return {
      getUser: getUser, 
      getRepos: getRepos, 
      getRepoDetails: getRepoDetails,
      getRepoCollaborators: getRepoCollaborators
      }]
    return 
  app = angular.module("githubViewer")
  app.provider("githubProvider", [githubProvider])
  return

JavaScript:

  (function() {
    var app, githubProvider;
    githubProvider = function() {
      var getRepoCollaborators, getRepoDetails, getRepos, getUser, http;
      http = null;
      getUser = function(username) {
        return http.get("https://api.github.com/users/" + username).then(function(response) {
          return response.data;
        });
      };
      getRepos = function(user) {
        return http.get(user.repos_url).then(function(response) {
          return response.data;
        });
      };
      getRepoDetails = function(username, repoName) {
        return http.get("https://api.github.com/repos/" + username + "/" + repoName).then(function(response) {
          return response.data;
        });
      };
      getRepoCollaborators = function(repo) {
        return http.get(repo.contributors_url).then(function(response) {
          return response.data;
        });
      };
      this.$get = [
        "$http", function($http) {
          http = $http;
          return {
            getUser: getUser,
            getRepos: getRepos,
            getRepoDetails: getRepoDetails,
            getRepoCollaborators: getRepoCollaborators
          };
        }
      ];
    };
    app = angular.module("githubViewer");
    app.provider("githubProvider", [githubProvider]);
  })();

【问题讨论】:

  • 为什么需要provider,这里没有配置
  • 是的,我不需要这个作为提供者,但我的问题更多是试图找出注入提供者的正确方法。

标签: javascript angularjs coffeescript


【解决方案1】:

正如 AngularJS Developer's Guide 所说:

仅当您想公开 API 时才应使用 Provider 配方 对于应用程序范围的配置,必须在 应用程序启动

从我在您的代码中看到的,大部分功能只能在配置阶段之后使用。您有两种选择。

[1] 如果您在配置阶段没有任何需要设置的配置,那么如何考虑创建服务而不是提供程序。

.service('github', ['$http', function($http) {
      this.getUser = function(username) {
        return $http.get("https://api.github.com/users/" + username).then(function(response) {
          return response.data;
        });
      };
      this.getRepos = function(user) {
        return $http.get(user.repos_url).then(function(response) {
          return response.data;
        });
      };
      this.getRepoDetails = function(username, repoName) {
        return $http.get("https://api.github.com/repos/" + username + "/" + repoName).then(function(response) {
          return response.data;
        });
      };
      this.getRepoCollaborators = function(repo) {
        return $http.get(repo.contributors_url).then(function(response) {
          return response.data;
        });
      };
}]);

[2] 如果您有任何配置,则只需复制上面的服务并将其定义在提供商的$get 中。

.provider('github', function() {
      var configuration = {};

      this.setConfiguration = function(configurationParams) {
         configuration = configurationParams;
      };

      this.$get = ['$http', function($http) {
        // you can choose to use your configuration here..

        this.getUser = function(username) {
          return $http.get("https://api.github.com/users/" + username).then(function(response) {
            return response.data;
          });
        };
        this.getRepos = function(user) {
          return $http.get(user.repos_url).then(function(response) {
            return response.data;
          });
        };
        this.getRepoDetails = function(username, repoName) {
          return $http.get("https://api.github.com/repos/" + username + "/" + repoName).then(function(response) {
            return response.data;
          });
        };
        this.getRepoCollaborators = function(repo) {
          return $http.get(repo.contributors_url).then(function(response) {
            return response.data;
          });
        };
      }];
});

然后可以在配置阶段使用此提供程序,如下所示:

.config(function(githubProvider) {
  githubProvider.setConfiguration({
    dummyData: 'Dummy Data'
  });
});

在运行阶段或在控制器中:

.run(function(github) {
  github.getUser('ryebalar').then(function(data) {
    console.log(data);
  });
});

这里有一份指南可帮助您与供应商联系,developer's guide,请注意我上面提供的报价来自该指南。

【讨论】:

    猜你喜欢
    • 2016-04-23
    • 2018-08-21
    • 1970-01-01
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 2019-10-24
    • 2021-04-23
    • 1970-01-01
    相关资源
    最近更新 更多