[英]Angular passing parameters
我正在尝试学习AngularJs,并且正在观看课程,下面的图像中显示了此代码。 作者正在做$http.get("url").then(function()...
我不明白的是onUserComplete
函数接受response
参数,但是在then
函数中,他没有传递该response
参数,从我对JavaScript的理解来看,我们应该这样做: then(onUserComplete(response)) ;
有人可以替我解释一下吗?
我建议您再次浏览文档: https : //docs.angularjs.org/api/ng/service/ $ http
但总而言之:
然后基本上等待诺言返回,这意味着来自服务器的响应到达了。
响应包含参数数据和来自服务器的数据(假设为json)
然后将expect函数作为参数,以便将函数名称替换为函数,在您的情况下为onUserComplete。
看看类似的例子:
// Simple GET request example :
$http.get('/someUrl').
success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
then
函数将调用带有响应(实际上是数据)对象的回调。
.then()
函数将回调函数作为其参数。 回调函数存储在变量onUserComplete
。 因此,当作者编写.then(onUserComplete)
,不会调用onUserComplete
,它只是作为参考传递。
.then
期望将函数作为参数,这正是onUserComplete
。
用简单的语法,您会看到:
.then(function (response) {
$scope.user = response.data;
});
并且以更常见的方式,使用匿名语法:
.then(response =>
$scope.user = response.data;
);
这就是全部。 onUserComplete
只是替代品。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.