Я столкнулся с ошибкой, вызванной угловыми вызовами, вызывающими службу отдыха. Подробности ниже.угловая ошибка бесконечной итерации js
Вот мой app.js код:
angular.module('myApp', []);
angular.module('myApp').factory('weatherService',function($http){
return {
getWeather:function(city,country){
var query = 'city=' + city + '&country=' + country;
return $http.get('http://api.openweathermap.org/data/2.5/weather',{
params: {
q:query
}
}).then(function(response){
return response.data.weather[0].description;
});
}
}
});
angular.module('myApp').controller('WeatherController',
function($scope,weatherService){
$scope.getWeather=function(city,country){
$scope.WeatherDescription = "Fetching...";
weatherService.getWeather(city,country).then(function(data){
$scope.weatherDescription = data;
}, function(data){
$scope.weatherDescription = "Could not obtain data";
});
}
});
HTML код:
<html ng-app="myApp">
<script src="../js/angular/angular.js" ></script>
<script src="../js/app.js" ></script>
<body ng-controller="WeatherController">
{{getWeather('chicago','usa')}}
</body>
</html>
я получаю пустой ответ на странице. Когда я открываю консоль, я получаю эту ошибку:
Watchers fired in the last 5 iterations: []
http://errors.angularjs.org/1.3.13/$rootScope/infdig?p0=10&p1=%5B%5D
at angular.js:63
at Scope.$digest (angular.js:14281)
at Scope.$apply (angular.js:14506)
at done (angular.js:9659)
at completeRequest (angular.js:9849)
at XMLHttpRequest.requestLoaded (angular.js:9790)
(anonymous) @ angular.js:11607
(anonymous) @ angular.js:8557
$apply @ angular.js:14508
done @ angular.js:9659
completeRequest @ angular.js:9849
requestLoaded @ angular.js:9790
angular.js:63 Uncaught Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
что может быть неправильным? Кто-нибудь может помочь? Я использую версию 1.6.1, кстати,
Спасибо.
Я думаю, это потому, что вы вызвали функцию getWeather ('chicago', 'usa') 'от интерполяции. Попробуйте вызвать его в контроллере и показать результат в виде (HTML). – SaiUnique
Что вы имеете в виду назвать интерполяцией? –
Звонок с точки зрения не проблема, он будет работать, проблема что-то другое –