Javascript 一个承诺怎么会失败
有两个休息电话Javascript 一个承诺怎么会失败,javascript,angularjs,Javascript,Angularjs,有两个休息电话 http://localhost:8080/sample/rest/ser/person/list2/two 首先是 http://localhost:8080/sample/rest/ser/out/2 哪个回报 {"num":"2"} [{"personName":"Rahul Shivsharan","personId":123},{"personName":"Mehul Sarnikar","personId":34},{"personName":"Praful P
http://localhost:8080/sample/rest/ser/person/list2/two
首先是
http://localhost:8080/sample/rest/ser/out/2
哪个回报
{"num":"2"}
[{"personName":"Rahul Shivsharan","personId":123},{"personName":"Mehul Sarnikar","personId":34},{"personName":"Praful Patel","personId":343}]
第二个休息电话
http://localhost:8080/sample/rest/ser/person/list2/two
哪个回报
{"num":"2"}
[{"personName":"Rahul Shivsharan","personId":123},{"personName":"Mehul Sarnikar","personId":34},{"personName":"Praful Patel","personId":343}]
我需要做的是根据第一个rest的响应调用第二个rest调用
我已经写了一个angularjs代码
具体如下
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<script type="text/javaScript" src="../scripts/angular.js"></script>
<script src="../scripts/angular-resource.js"></script>
<script type="text/javascript">
var myApp = angular.module("myApp",['ngResource']);
myApp.factory("CountService",function($resource){
return $resource("../rest/ser/out/:num",{
num : "@num"
});
}).factory("PersonService",function($resource,CountService,$interval){
var obj = new Object();
obj.getPersonList = function(inputTxt){
return CountService.get({
"num" : inputTxt
}).$promise.then(function(response){
var url = "../rest/ser/person/list2/";
if(response.num === 2){
url += "two";
}else if(response.num === 3){
url += "three";
}else{
url += "other";
}
return $resource(url,{});
});
};
return obj;
}).controller("myCtrl",function($scope,PersonService){
$scope.getInfo = function(){
PersonService.getPersonList($scope.inputTxt).query().$promise.then(function(response){
$scope.personList = response;
});
}
});
</script>
</head>
<body ng-controller="myCtrl">
<h1>{{num}}</h1>
<br />
<input type="text" ng-model="inputTxt" /><br/><br/>
<input type="button" value="ENTER" ng-click="getInfo()" />
<br /><br /><br />
<table border="2">
<tr ng-repeat="per in personList">
<td>{{per.personName}}</td>
<td>{{per.personId}}</td>
</tr>
</table>
</body>
在此处插入标题
var myApp=angular.module(“myApp”,['ngResource']);
myApp.factory(“CountService”,函数($resource){
返回$resource(“../rest/ser/out/:num”{
num:“@num”
});
}).factory(“PersonService”,函数($resource,CountService,$interval){
var obj=新对象();
obj.getPersonList=函数(inputXT){
返回CountService.get({
“num”:输入文本
}).$PROMITE.then(功能(响应){
var url=“../rest/ser/person/list2/”;
如果(response.num==2){
url+=“两个”;
}else if(response.num==3){
网址+=“三”;
}否则{
url+=“其他”;
}
返回$resource(url,{});
});
};
返回obj;
}).controller(“myCtrl”,函数($scope,PersonService){
$scope.getInfo=function(){
PersonService.getPersonList($scope.inputxt.query().$promise.then(函数(响应))的{
$scope.personList=响应;
});
}
});
{{num}}
{{per.personName}
{{per.personId}
它所做的是在用户输入的基础上进行第一次rest调用,
在第一次rest调用成功后,将进行第二次rest调用
所以第一个rest调用返回“2”,我将其转换为“2”,并附加到第二个rest调用的url
但这里我得到了一个错误
是的
TypeError: PersonService.getPersonList(...).query is not a function
at Scope.$scope.getInfo (index.html:65)
at Parser.functionCall (angular.js:10795)
at angular.js:19036
at Scope.$get.Scope.$eval (angular.js:12632)
at Scope.$get.Scope.$apply (angular.js:12730)
at HTMLInputElement.<anonymous> (angular.js:19035)
at angular.js:2843
at forEach (angular.js:325)
at HTMLInputElement.eventHandler (angular.js:2842)
TypeError:PersonService.getPersonList(…)。查询不是函数
位于Scope.$Scope.getInfo(index.html:65)
at Parser.functionCall(angular.js:10795)
在angular.js:19036
范围为$get.Scope.$eval(angular.js:12632)
在作用域$get.Scope.$apply(angular.js:12730)
在HTMLInputElement。(angular.js:19035)
在angular.js:2843
在forEach(angular.js:325)
在HTMLInputElement.eventHandler(angular.js:2842)
我认为承诺本身就是一个承诺
所以我的代码应该可以工作,但它不能
我哪里做错了
你能给我一个更好的解决方案吗?你的问题是
PersonService.getPersonList
不返回一个名为query
的(函数)属性的对象。我不知道getPersonList
返回了什么,但是如果它是一个承诺,那么它应该仍然会失败,因为承诺在angular中没有query
函数。就像@AlexMA说的,你返回的是一个
最简单的方法是在调用.query()
之前提供一个额外的语句:
下面是一个it实际应用的示例: