Angular Provider中的依赖注入

use*_*197 5 javascript coffeescript angularjs

我想知道是否有更清洁的方式注入提供者.正如我现在所做的那样,我必须拥有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
Run Code Online (Sandbox Code Playgroud)

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]);
  })();
Run Code Online (Sandbox Code Playgroud)

rye*_*lar 2

正如AngularJS 开发者指南提到的:

仅当您想要公开 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;
        });
      };
}]);
Run Code Online (Sandbox Code Playgroud)

[ 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;
          });
        };
      }];
});
Run Code Online (Sandbox Code Playgroud)

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

.config(function(githubProvider) {
  githubProvider.setConfiguration({
    dummyData: 'Dummy Data'
  });
});
Run Code Online (Sandbox Code Playgroud)

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

.run(function(github) {
  github.getUser('ryebalar').then(function(data) {
    console.log(data);
  });
});
Run Code Online (Sandbox Code Playgroud)

这是帮助您找到提供商的指南,即开发人员指南,请注意,我上面提供的引用来自该指南。