2013-07-21 41 views
0

我需要一點幫助來構建我的角度應用程序。我有兩個控制器'OfferListCntrl'(顯示優惠清單)和'OfferDetailsCntrl'(通過ID顯示單個優惠)以及名爲'Offers'的服務/模型。他們分別通過Offer服務,GetOffers(返回提供數組)和GetOffer(返回單個提議)連接到兩個Web服務,並且如果GetOffers已被調用(保存服務調用),我試圖避免調用GetOffer(id)角度視圖不能從共享服務更新

MyApp.factory('Offers', function($http,Config) { 
var data=[]; 
var Offers = { 
    get: function() { 
     var promise = $http.get(Config.API_END_POINT + "GetOffers?appID=12",{cache:"true"}) 
     .then(function (response){ 
      data = response.data; 
      return response.data; 
     }); 
     return promise; 
    }, 
    getOffer: function(id) { 
     var local = _.find(data.Offers, function(offer) { return offer.OfferID == id });     
     if(local){ 
      console.log("local copy found:" + local.OfferID); 
      console.log(local.Description); 
      console.log(local); 
      return local; 
     } 
     else { 
      var promise = $http.get(Config.API_END_POINT + "GetOffer?appID=12&OfferID=" + id,{cache:"true"}) 
      .then(function (response){ 
       console.log("no local, calling ws"); 
       return response.data; 
      }); 
      return promise; 
     } 

    } 
}; 
return Offers; 
}); 



function OfferDetailCtrl($scope,$http,Offers) { 
    Offers.get().then(function (asyncData){ 
     $scope.offers = asyncData.Offers; 
    }); 
}]); 


function OfferDetailCtrl($scope,$routeParams,$http,$location,Offers) { 
    $scope.offerId = $routeParams.offerId; 
    Offers.getOffer($scope.offerId).then(function (asyncData){ 
     $scope.offer = asyncData; 
    }); 
} 

1個問題。

  1. 麻煩的是,當Offers.getOffer(ID)被調用:它似乎發現羯羊它具有本地數據,或者未如預期,如果它調用web服務,一切都很好沒有本地數據,但如果有現有的數據,它會嘗試將其返回給OfferDetailCtrl然後出錯。我可以console.log它的內容就在返回之前,它似乎完好無損,所以我很困惑爲什麼控制器會接受承諾,但不是實際的對象。

Error: 'undefined' is not a function (evaluating 'Offers.getOffer($scope.offerId).then(function (asyncData){ $scope.offer = asyncData; console.log($scope.offer); })') [email protected]http://staging.scanzap.com.au/assets/js/offers/OfferDetailController.js:17

,如果有更好的方法我可以做這個ID喜歡聽:)感謝您的幫助

+0

答案是一個服務...我應該買域名theanswerisaservice.com它是經常在AngularJS的SO問題上的答案。如果您需要在應用程序的整個生命週期中維護某些對象的持久狀態(爲了共享數據而不創建額外請求),服務就是解決方案。 http://www.youtube.com/watch?v=ZhfUv0spHCY&t=26m40s <我建議在有空時多次觀看整個演講。 – shaunhusain

+0

如果一個服務對你的控制器之間的通信沒有意義,另一個選擇是使用事件($ on,$ broadcast,$ emit)在子/父範圍控制器之間進行通信。 – shaunhusain

+0

感謝您的回覆,並檢查了鏈接。不過,我認爲這就是我已經在'提供'中做了什麼,並且正在將它注入到兩個控制器中,如上面代碼中所示。提供的是我上面定義的(使用工廠方法)而不是服務? – owlyfool

回答

0

嘗試做這樣的事情,而不是一定要注資$ Q以及。

getOffer: function(id) { 
     var deferred = $q.defer(); 
     var local = _.find(data.Offers, function(offer) { return offer.OfferID == id });     
     if(local){ 
      console.log("local copy found:" + local.OfferID); 
      console.log(local.Description); 
      console.log(local); 
      deferred.resolve(local;) 
     } 
     else { 
      var promise = $http.get(Config.API_END_POINT + "GetOffer?appID=12&OfferID=" + id,{cache:"true"}) 
      .then(function (response){ 
       console.log("no local, calling ws"); 
       deferred.resolve(response.data); 
      }); 
     } 

     return deferred.promise; 

    } 
+0

謝謝你,我在想這是沿着這些線。在新OfferDetailCtrl (Safari瀏覽器:) Error對象#有沒有方法「那麼」 :不幸的是我仍然得到一個錯誤 (鉻:) 類型錯誤「未定義」不是一個函數(評估的報價.getOffer($ scope.offerId)。然後(函數(asyncData){ \t //console.log("async數據」,asyncData); \t $ scope.offer = asyncData; })') – owlyfool

+0

這是如何其被稱爲...我親自實施你的解決方案或離開它,因爲我得到相同的錯誤 '代碼' .getOffer($ scope.offerId).then(function(asyncData){0} {0} {0} {0}。offer = asyncData; }); 'code' – owlyfool

+0

啊,不得不返回myPromise.promise讓它工作,編輯以反映變化,非常感謝您的幫助。 – owlyfool