2015-05-02 28 views
5

AngularJS'den bir WebAPI aramaya çalışıyorum. Basit bir GET isteği ve sunucuda CORS'i etkinleştirdim.AngularJS enjektör sorunu

$ enjektörünü alıyorum: unpr Bilinmeyen Sağlayıcı hatası.

I raterModule.js olarak adlandırılan bir açısal modülü vardır:

var raterModule = angular.module('rater', []); 

bir servis olarak adlandırılan enable-cors.org gelen pasajı kullanan corsService.js:

raterModule.factory("corsService", 
function createCORSRequest(method, url) { 
    var xhr = new XMLHttpRequest(); 
    // Check if the XMLHttpRequest object has a "withCredentials" property. 
    // "withCredentials" only exists on XMLHTTPRequest2 objects. 
    if ("withCredentials" in xhr) { 
     xhr.withCredentials = true; 
     xhr.open(method, url, true); 
    } 
    // Otherwise, check if XDomainRequest. XDomainRequest only exists in IE, and is IE's way of making CORS requests. 
    else if (typeof XDomainRequest != "undefined") {    
     xhr = new XDomainRequest(); 
     xhr.open(method, url); 
    } 
    // Otherwise, CORS is not supported by the browser. 
    else {    
     xhr = null; 
    } 
    return xhr; 
} 

)

ve son olarak menuController.js adlı bir denetleyici:

raterModule.controller("menuCtrl",["$scope","$http","corsService", 
    function menuCtrl($scope, $http, corsService) { 
     var xhr = corsService.createCORSRequest('GET', 'http://localhost:50942/api/menu/items'); 
     if (!xhr) { 
      throw new Error('CORS not supported'); 
     } 
     // Getting menu items 
     $http.get(xhr) 
        .success(function (data, status, headers, config) { 
         $scope.menu = data; 
        }) 
        .error(function (data, status, headers, config) { 
         alert("error getting menu items"); 
        }); 
    } 
]); 

Bunların hepsi bir HTML endeksi sayfanın alt kısmında yer almaktadır. Ben corsService menuCtrlenjekte edilir ve sonra menuCtrlraterModule eklenir olurdu gerçekleşmesi umuyordum ne.

MenüCtrl, daha sonra aynı ilke sorununu ortadan kaldırarak WebAPI'ye gönderilen bir istek oluşturmak için corsService'i kullanır.

Eminim bu basit bir şeydir ama cehaletimde bunu göremiyorum.

+0

HTML kodunuzu ve ayrıca tam hata mesajı yapıştırmak miyim? –

cevap

2

Açılama, method ve url sağlayıcıları, createCORSRequest işlevlerinize değil, işlev parametrelerine enjekte etmenizi beklediğiniz için bir hatadır.

Yani, şu şekilde senin corsService inceleyebilirsiniz:

raterModule.factory(`corsService`, function() { 
    var service = {}; 

    service.createCORSRequest = fucntion(method, url) { 
    // your logic here 
    }; 

    return service; 
}) 
+0

Teşekkürler, şimdi çalışıyor. – VenerableChris