Javascript 不赞成的修订
我正在阅读一本关于使用平均堆栈创建应用程序的书。我已经完全遵循了代码,但是我得到了一个错误,因为这本书在删除$http success方法之前使用了Angular的旧版本。我想继续从书中学习,但在编辑代码以使用新的.then版本的$http时遇到了问题。我现在有Javascript 不赞成的修订,javascript,angularjs,node.js,Javascript,Angularjs,Node.js,我正在阅读一本关于使用平均堆栈创建应用程序的书。我已经完全遵循了代码,但是我得到了一个错误,因为这本书在删除$http success方法之前使用了Angular的旧版本。我想继续从书中学习,但在编辑代码以使用新的.then版本的$http时遇到了问题。我现在有 var locationListCtrl = function ($scope, loc8rData) { $scope.message = "Searching for nearby places"; loc8rData
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(…)中,谢谢省略号看起来只是占位符(即“在此处实现代码…”),不用担心。无论如何,解析for数据时会调用
.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');
};
用URL替换点。请参阅docs://docs.angularjs.org/api/ng/service/$http#一般用法。。。只是一个占位符。很棒的东西,谢谢。接球部分特别有用。