0
我想在第一個控制器中獲取http請求結果。 http請求由另一個控制器觸發。我有的問題是我不知道如何檢測請求是否在我的第一個控制器中完成。我有一些像如何在我的例子中獲取http請求數據?
第一控制器:
//I am not sure how to get the customer result if
//http requests are trigger by another controllers here.
customerFactory.getCustomerResult????
第二個控制器:
//trigger the http request..
var id = 1;
$scope.clickme = function() {
var obj = customerFactory.callApi(id)
}
我廠
customerFactory.callApi = function(id) {
return getCustomer(id)
.then(function(customer) {
return customer;
})
}
var getCustomer = function(id) {
return $http.get('/api/project1/getCustomer' + id);
}
return customerFactory;
HTML
<div ng-controller="firstCtrl">
//codes...
</div>
//other codes..
//other codes..
<div ng-controller="secondCtrl">
//codes...
</div>
第一個和第二個控制器不相關。他們彼此遠離。如何讓firstCtrl檢測到http請求已完成並獲取客戶數據?非常感謝!
謝謝shaunhusain,你的例子顯示了一個控制器調用的是ExampleService.getData的調用,但它如何將返回數據傳遞給另一個控制器? +1 – BonJon 2014-11-02 02:45:20
剛剛編輯顯示第二個控制器,它與第一個控制器是一樣的,只是我正在做的和你在做什麼之間的真正區別是我還將數據存儲在服務中(工廠也很好)所以這種方式可以從兩個地方引用。有一些選擇,但通常這是我遇到的最好的方式。也是公平的,我從這個其他答案我的代碼,但我有這個問題很多次,並提到人們這個其他帖子http://stackoverflow.com/questions/17667455/angular-http-vs-service-vs- ngresource – shaunhusain 2014-11-02 02:48:40
感謝您的幫助!我的問題是我需要在ExampleCtrl中觸發http請求,但讓ExampleCtrl2獲取數據。如果我理解正確,ExampleCtrl2需要調用getData來獲取數據。我只想要ExampleCtrl來調用getData方法。 – BonJon 2014-11-02 03:00:14