2014-09-13 18 views
5

मुझे आश्चर्य है कि प्रदाता में इंजेक्शन देने का कोई क्लीनर तरीका है या नहीं। जैसा कि मैं अब कर रहा हूं, मुझे http = null होना है, और उसके बाद http = $ http को $ में सेट करें ताकि मैं इसे अपने कार्यों में उपयोग कर सकूं।कोणीय प्रदाता में निर्भरता इंजेक्शन

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 

जावास्क्रिप्ट:

(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]); 
    })(); 
+0

आपको प्रदाता की आवश्यकता क्यों है, यहां कोई कॉन्फ़िगरेशन नहीं है – calebboyd

+0

हां, मुझे प्रदाता होने की आवश्यकता नहीं है, लेकिन मेरा प्रश्न प्रदाताओं में इंजेक्शन करने का उचित तरीका जानने का प्रयास करने का अधिक प्रयास था। – user4029197

उत्तर

2

के रूप में क्या AngularJS Developer's Guide उल्लेख किया:

आप प्रदाता नुस्खा केवल का उपयोग करना चाहिए नीचे प्रदाता के लिए अपने कोड

CoffeeScript है जब आप एपीआई का पर्दाफाश करना चाहते हैं आवेदन चौड़ा विन्यास कि पहले आवेदन

शुरू होता है कि मैं क्या देख अपने कोड में, कार्यों का सबसे केवल विन्यास चरण के बाद इस्तेमाल किया जा सकता से किया जाना चाहिए। आपके पास विचार करने के लिए दो विकल्प हैं।

[] यदि आपके पास कॉन्फ़िगरेशन चरण के दौरान सेटअप करने की आवश्यकता नहीं है, तो प्रदाता के बजाय सेवा बनाने पर विचार करने के बारे में कैसे करें।

.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; 
     }); 
     }; 
}]); 

[] क्या आप किसी भी विन्यास की क्या ज़रूरत है, तो बस सेवा से ऊपर कॉपी करें और प्रदाता के $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, ध्यान दें कि मैंने जो उद्धरण दिया है वह उस गाइड से है।

संबंधित मुद्दे