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

相關問題