服务和控制器以 api 末尾的值调用 api

Service and Controller to call api with a value in the end of api

我有一个服务调用,我想将一个值传递给函数以通过 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), 

但我在

时看到了 id
console.log($scope.orderid);

您应该使用 .then 而不是 .success。来自 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.

.success 方法破坏 .then 的响应对象:

Service.getOrderbyorderid($scope.orderid)
// .success(function (data) {
   .then(function (response) {
     $scope.ordertest = response.data;