我有一个服务调用,我想将值传递给函数以通过id调用http.get api:
/home/api/order/1我的服务看起来像这样:
angular.module("app") .factory('Service', ['$http', function ($http) { var urlBase = '/home/api/order/'; Service.getOrderbyorderid = function (orderid) { return $http.get(urlBase + orderid) }; });我为我的控制器试过的是:
getOrderbyorderid (); function getOrderbyorderid () { $scope.orderid = $routeParams.orderid ; console.log($scope.orderid); //Sees ID Service.getOrderbyorderid ($scope.orderid ) .success(function (response) { $scope.ordertest = [] $scope.ordertest = response.data; console.log($scope.ordertest ); //Undefined }) };题
有人可以帮我调用控制器,所以我可以用特定的orderid调用api吗?
我得到的错误
我什么时候得到一个未定义的
console.log($scope.ordertest),但我看到了我的身份
console.log($scope.orderid);I have a service call that I want to pass a value into the function to call the http.get api by id:
/home/api/order/1My service looks like this:
angular.module("app") .factory('Service', ['$http', function ($http) { var urlBase = '/home/api/order/'; Service.getOrderbyorderid = function (orderid) { return $http.get(urlBase + orderid) }; });What I have tried for my Controller is:
getOrderbyorderid (); function getOrderbyorderid () { $scope.orderid = $routeParams.orderid ; console.log($scope.orderid); //Sees ID Service.getOrderbyorderid ($scope.orderid ) .success(function (response) { $scope.ordertest = [] $scope.ordertest = response.data; console.log($scope.ordertest ); //Undefined }) };Question
Can someone help me in calling the controller right so I can call the api with a specific orderid?
Error I am getting
I am getting an undefined when I
console.log($scope.ordertest),but I see the id when I
console.log($scope.orderid);最满意答案
你应该使用.success而不是.success 。 从角度文档 :
$ http遗留承诺方法成功和错误已被弃用。 请改用标准方法。 如果$ httpProvider.useLegacyPromiseExtensions设置为false,则这些方法将抛出$ http / legacy错误。
.success方法破坏.success的响应对象:
Service.getOrderbyorderid($scope.orderid) // .success(function (data) { .then(function (response) { $scope.ordertest = response.data;You should use .then rather than .success. From the angular docs:
The $http legacy promise methods success and error have been deprecated. Use the standard then method instead. If $httpProvider.useLegacyPromiseExtensions is set to false then these methods will throw $http/legacy error.
The .success method destructs the response object for .then:
Service.getOrderbyorderid($scope.orderid) // .success(function (data) { .then(function (response) { $scope.ordertest = response.data;更多推荐
发布评论