2014-09-27 71 views
0

定義我有3個文件:

app.js

angular.module("starweb", ["ngRoute", "ngAnimate", "ui.bootstrap"]) 
     .config(function ($routeProvider) { 
     $routeProvider .... 

addHostingController.js

angular.module("starweb") 
    .controller("addHostingCtrl", function ($scope, domainService) { 
     $scope.data.domains = domainService.getDomain(); 
} 

domainService.js

angular.module("starweb") 
.constant("domainList", "http://localhost:15536/api/product/xxxx") 
.service("domainService", function ($http, $q,domainList) { 
    return ({ 
     getDomain: GetDomains() 
    }); 
}); 

function GetDomains() { 
    $http.get(domainList).then(handleSuccess, handleError); 
} 

在頁面源代碼中,包含所有3個文件(首先是app.js)。 Chrome顯示$http is undefined,我的設置有什麼問題?

謝謝大家。

回答

2

GetDomains功能無法訪問domainService範圍,在此範圍內已注入$http服務。

您應該更改您的代碼是這樣的:

.service("domainService", function ($http, $q, domainList) { 
    return { 
    getDomain: function (handleSuccess, handleError) { 
     $http.get(domainList).then(handleSuccess, handleError); 
    } 
    }; 
}); 

你也應該定義handleSuccesshandleError回調,想必作爲getDomain函數的參數。

+0

哦謝謝,我的壞。 – 2014-09-27 10:32:01