javascript - How a promise returning a promise fails -
there 2 rest call
the first
http://localhost:8080/sample/rest/ser/out/2
which return
{"num":"2"}
the second rest call
http://localhost:8080/sample/rest/ser/person/list2/two
which return
[{"personname":"rahul shivsharan","personid":123},{"personname":"mehul sarnikar","personid":34},{"personname":"praful patel","personid":343}]
what need on basis of response of first rest need invoke second rest call.
i have written 1 angularjs code
which follows
<!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>
what on basis of input enter user first rest call made, , after success of first rest call second rest call made.
so first rest call returns "2", convert "two" , append url of second rest call.
but here getting error
it
typeerror: personservice.getpersonlist(...).query not function @ scope.$scope.getinfo (index.html:65) @ parser.functioncall (angular.js:10795) @ angular.js:19036 @ scope.$get.scope.$eval (angular.js:12632) @ scope.$get.scope.$apply (angular.js:12730) @ htmlinputelement.<anonymous> (angular.js:19035) @ angular.js:2843 @ foreach (angular.js:325) @ htmlinputelement.eventhandler (angular.js:2842)
what thought promise returns promise itself,
so code should work, doesn't,
where going wrong.
can u suggest me better solution
like @alexma said, returning $resource
"class" object.
the easiest way things working provide .then
statement prior calling .query()
:
$scope.getinfo = function(){ personservice.getpersonlist($scope.inputtxt) .then(function(resource) { return resource.query().$promise; }) .then(function(response){ $scope.personlist = response; }) ; };
here example of in action: http://plnkr.co/edit/p00rbuk1ld8eo0vd6oyn?p=preview
Comments
Post a Comment