Pracuję nad projektem jonowym, a ja go zdobywamdane z interfejsu API REST za pośrednictwem usługi $ http. Robię to przez rozwiązanie w trasie, która przynosi obietnicę z usługi i wprowadza do kontrolera. Moja funkcja doRefresh znajduje się w tym kontrolerze, jednak rozwiązanie znajduje się w obrębie trasy, co może wątpić.
Mój trasa jest tutaj:
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state("home", {
url: "/",
templateUrl: "templates/home.html",
controller: "MainCtrl as vm",
resolve: {
weather: function(MyService) {
return MyService.getData();
}
});
$urlRouterProvider.otherwise("/");
});
Mój usługa jest tutaj
.factory("MyService", function($http) {
function getData() {
return $http.get("http://something.api?format=json")
.then(function(res) {
return res.data;
});
}
return {
getData: getData()
};
});
I wreszcie, oto moje kontroler
.controller("MainCtrl", function(weather, $ionicLoading) {
var vm = this;
vm.weather = weather;
vm.doRefresh = function() {
// how can I resolve the promise here?? It is right? Is this the correct architecture?
}
});
Dzięki za pomoc!
Odpowiedzi:
1 dla odpowiedzi № 1Dobrze, weather
powinien zwrócić obietnicę. Tak, możesz to zrobić w swoim kontrolerze:
vm.weather.then(function(){
...
})