【发布时间】:2014-11-02 11:06:58
【问题描述】:
我正在尝试在我的第一个控制器中获取 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 请求是否完成并获取客户数据?非常感谢!
【问题讨论】:
标签: javascript angularjs