【发布时间】:2017-02-13 13:34:44
【问题描述】:
我知道我问这个时看起来很愚蠢,但我无法弄清楚这一点。
我编写了一个服务来处理对服务器的 post 调用。 $q 服务将承诺返回给调用该服务的控制器函数。
服务:
app.service('AjaxService', ['$http','$q','$log', function($http,$q,$log) {
return {
getSearchresultPost : function(url,data){
var defer = $q.defer();
$http.post(url, data)
.then(function(data, status, header, config){
defer.resolve(data);
}).then(function(data, status, header, config){
defer.reject(data);
});
return defer.promise;
}
};
}]);
控制器
app.controller("kitGuideNavigationController",['$scope','$window','$timeout','AjaxService',function($scope,$window,$timeout,AjaxService){
AjaxService.getSearchresultPost("/services/knowledge/getProducts",pathToCall)
.then(function(data){
console.log("Data ",data);
}).then(function(data){
console.log("Some Error Occured");
});
}]);
当我尝试运行代码时,我会打印两个控制台。
我不明白出了什么问题。有人可以帮忙吗?
【问题讨论】:
-
您可以使用
$http.post()返回的差异,除非您有嵌套调用等花哨的东西,否则无需创建自定义差异
标签: angularjs angularjs-directive angularjs-scope angular-ui-router