2016-02-05 78 views
-1

我想在平均堆栈上创建简单的webapp。AngularJS请求模型创建另一个模型之前[NodeJS]

我想创建属于博览会的提议,但不知道如何在调用createOffer之前请求博览会。

这里是我的代码

​​

和控制器

offerApp.controller('OffersController', ['$scope', '$resource', '$state', '$location', 'OfferUpdateService', 'Upload', 
    function ($scope, $resource, $state, $location, OfferUpdateService, Upload) { 
     var OfferResource = $resource('/offer/:id'); 
     var ExpositionResource = $resource('/exposition/:id'); 
     $scope.offerUpdateService = new OfferUpdateService(); 
     var loadOffers = function() { 
     return OfferResource.query(function (results) { 
      $scope.offers = results; 
      if ($state.params.id) { 
      $scope.findOffer($state.params.id); 
      } 
      if ($state.params.expId) { 
      ExpositionResource.findExposition($state.params.expId); 
      } 
     }); 
     }; 
    } 
]); 

它是正确的思想?我想要在提供之前加载展览,然后将exposition.id映射到提供模型。

谢谢。

+0

是否存在特定问题?问题比较模糊 – charlietfl

+0

@charlietfl问题是如何在创建子项之前加载父模型。 ExpositionResource.findExposition($ state.params.expId); - 此代码不起作用 –

+0

*“不起作用”*不是正确的问题描述。我们不知道是否有请求,如果抛出错误,findOffer()是什么或失败。从开发工具控制台提供更好的疑难解答细节 – charlietfl

回答

2

您需要使用承诺链来执行此操作。我并不完全清楚你的资源对象是如何工作的(因为我使用$ q & $ http代替,但文档指出它们使用RESTful API返回对象)。以下是按顺序请求2个资源的示例:

var OfferResource = $resource('/offer/:id'); 
var ExpositionResource = $resource('/exposition/:id'); 
ExpositionResource.get({id:123}).$promise.then(function(rsp, rspHeaders){ 

    //set your model ID how you want 
    model.id = rsp.id 

    //now hit your next API in the sequence 
    OfferResource.query({id:model.id}).$promise.then(function(rsp2, rspHeaders2){ 
     //you can do this again if need be to a 3rd sequential call 
    }) 
}) 
+0

帮我!谢谢! –

+0

乐于助人。 – jusopi

相关问题