【发布时间】:2015-12-02 21:12:48
【问题描述】:
我希望两个不同的控制器在服务中解决某些承诺后运行不同的功能(我不希望该服务在每次控制器需要数据时都发出 http 请求,我只想要一个 http 请求)。
我有一个服务,它发出请求并得到一个承诺。我希望 controller1 看到这个分辨率,然后运行一些代码。然后我希望控制器 2 也看到这个承诺解决并运行一些代码(基本上是多个 then() 方法,它们在同一个承诺上运行但来自不同的文件)。我该怎么做?
我看到的所有示例在某个承诺解决后都有一个控制器运行代码,但没有多个控制器监听同一个承诺以解决。
这是我从本文中借用的一些代码(我会添加一个“母控制器”来说明我的示例,我不希望子服务进行两次 http 调用):http://andyshora.com/promises-angularjs-explained-as-cartoon.html
儿子服务
app.factory('SonService', function ($http, $q) {
return {
getWeather: function() {
// the $http API is based on the deferred/promise APIs exposed by the $q service
// so it returns a promise for us by default
return $http.get('http://fishing-weather-api.com/sunday/afternoon')
.then(function(response) {
if (typeof response.data === 'object') {
return response.data;
} else {
// invalid response
return $q.reject(response.data);
}
}, function(response) {
// something went wrong
return $q.reject(response.data);
});
}
};
});
父亲控制器:
// function somewhere in father-controller.js
var makePromiseWithSon = function() {
// This service's function returns a promise, but we'll deal with that shortly
SonService.getWeather()
// then() called when son gets back
.then(function(data) {
// promise fulfilled
if (data.forecast==='good') {
prepareFishingTrip();
} else {
prepareSundayRoastDinner();
}
}, function(error) {
// promise rejected, could log the error with: console.log('error', error);
prepareSundayRoastDinner();
});
};
主控制器:
var makePromiseWithSon = function() {
SonService.getWeather()
// then() called when son gets back
.then(function(data) {
// promise fulfilled
if (data.forecast==='good') {
workInTheGarden();
} else {
sweepTheHouse();
}
}, function(error) {
// promise rejected, could log the error with: console.log('error', error);
sweepTheHouse();
});
};
【问题讨论】:
-
只要它在相同的上下文中,并且您可以引用两个控制器中可用的承诺,这应该不是什么大问题,但是这个问题似乎很模糊要负责吗?
-
是的,只需创建多个使用来自服务的相同承诺的控制器。它可以开箱即用。
-
@Bergi,你能看看我的编辑吗?您是说我可以制作母控制器,而 sun 服务不会为此进行额外的 http 调用吗?基本上一个http请求,每个控制器都可以在解决后使用它?我关心的原因是因为请求正在执行大型查询。
-
不,他们不会 - (共享)服务不会每次都返回相同的承诺,每当您调用
getWeather时都会创建一个新的承诺(和请求)。 You can easily cache it 虽然。 -
在 http 请求上使用“cache: true”怎么样。这不是解决了多个请求的问题,而不必摆弄将 Promise 暴露给多个控制器吗?
标签: javascript angularjs q angular-promise angular-resource