【问题标题】:query a specific url with AngularJS and Express使用 AngularJS 和 Express 查询特定的 url
【发布时间】:2017-05-30 19:45:43
【问题描述】:

在我的控制器中,我调用了从后端获取文档的调用,如下所示:

orderFactory.query({_id: $stateParams.obj}).$promise.then(
function (response) {
    console.log(response);
    $scope.invoice = response[0].invoice;
    $scope.client = response[0].client;
    $scope.orderdetails = response[0].orderdetails;
},
function (response) {
    console.log(response);
    $scope.message = "Error: " + response.status + " " + response.statusText;        
});

但问题是这段代码向/orders&_id=5926bef0f5344c1ff8a9b295发送了一个GET请求,但它应该访问的REST端点是/orders/5926bef0f5344c1ff8a9b295

浏览器中的 URL 是 /trackdetails,我无法使用 $stateParams 访问所需的端点

所以我的问题是有什么方法可以从控制器访问该端点?或者我必须重新设计我的架构?

【问题讨论】:

    标签: angularjs rest express


    【解决方案1】:

    在资源中:

    $resource('your_url/:_id', 
      {
        _id: '@_id'
      }
    )
    

    在组件中:

    orderFactory.get({_id: $stateParams.obj}, function(response) {
      // success
    }, function(reject) {
      // error
    })
    

    【讨论】:

    • 似乎不起作用。这些更改导致了 /orders 调用,没有 _id 参数
    • 你能展示你的资源吗?因为,这正是您需要分配到资源中的内容。而且您实际上不需要显式设置 $promise,因为这正是 $resource 返回的内容。
    • .factory('orderFactory', ['$resource', 'baseURL', function ($resource, baseURL) { return $resource(baseURL + "orders/:id", null, { 'update': { method: 'PUT' } }); }]) 是我在更改任何内容之前的资源
    • 是的,这就是我要说的。您传递的是 null 而不是 { _id: '@_id' }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-20
    • 2019-07-14
    • 2021-12-21
    • 1970-01-01
    • 2015-02-11
    • 1970-01-01
    相关资源
    最近更新 更多