[英]Angular $http success deprecated amendments
我正在關注一本關於使用 MEAN 堆棧創建應用程序的書。 我已經到了完全遵循代碼的地步,但是我收到一個錯誤,因為這本書在刪除 $http 成功方法之前使用了舊版本的 Angular。 我想繼續從這本書中學習,但在編輯代碼以使用 $http 的新 .then 版本時遇到問題。 我目前有
var locationListCtrl = function ($scope, loc8rData) {
$scope.message = "Searching for nearby places";
loc8rData
.success(function(data) {
$scope.message - data.length > 0 ? "" : "No locations found";
$scope.data = { locations: data };
})
.error(function (e) {
$scope.message = "Sorry, Something's gone wrong";
});
};
var loc8rData = function ($http) {
return $http.get('/api/locations?lng=-0.79&lat=51.3&maxDistance=20');
};
我查看了這個站點,發現我需要將其更改為使用 $http 的新方式,即
$http(...)
.then(function onSuccess(response) {
// Handle success
var data = response.data;
...
}).catch(function onError(response) {
// Handle error
var data = response.data;
...
});
我是一個新手,所以如果這很明顯,我很抱歉,但我想知道如何准確修改代碼,以便我可以繼續閱讀本書教程。 特別是我不明白 $http(...) 中的 ... 謝謝
看起來省略號只是占位符(即“在此處實現您的代碼...”),不用擔心。 無論如何,當數據的Promise解析時, .then()
會被調用。
$http.get('https://www.google.com')
.then(function(response) {
console.log('Got response: ', response);
})
.catch(function(error) {
console.log('Got an error: ', error);
});
具體來說,在您的代碼片段中:
var locationListCtrl = function ($scope, loc8rData) {
$scope.message = "Searching for nearby places";
return loc8rData
.then(function(data) {
$scope.message = data.length > 0 ? "" : "No locations found";
$scope.data = { locations: data };
})
.catch(function(e) {
$scope.message = "Sorry, Something's gone wrong";
});
};
var loc8rData = function ($http) {
return $http.get('/api/locations?lng=-0.79&lat=51.3&maxDistance=20');
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.