【发布时间】:2017-07-30 14:54:53
【问题描述】:
我有点困惑,为什么 fetch 似乎不能正常工作,为什么 Angular 的 $http.get 在这种情况下可以工作。我的 fetch 请求正在通过,我可以看到我正在创建填充一些图像所需的数组。但它没有在我的 HTML 页面上填充任何内容。如果我在延迟一段时间后调用该函数两次,它似乎确实有效。如果我将其更改为 $http.get 它也可以。这是我的获取代码:
$scope.getPhotos = (url) => {
fetch(url).then(response => {
return response.json();
}).then(json => {
$scope.photoArray = json.photos;
console.log($scope.photoArray);
}).catch(err => {
alert('Error');
});
}
【问题讨论】:
-
我可以参考获取请求吗?
-
@VeeraBhadraRao 嘿。你的意思是我为什么要通过http使用它?主要原因是我认为它更干净,尤其是在处理 404 错误时。
-
@papernemesis Angular 1.x 我猜?你需要在你的
.then/.catch回调中以某种方式trigger a digest -
@CodingIntrigue 谢谢!完美运行。
标签: javascript angularjs get ecmascript-6 fetch