Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS如何从异步调用返回值?_Javascript_Angularjs_Asynchronous - Fatal编程技术网

Javascript AngularJS如何从异步调用返回值?

Javascript AngularJS如何从异步调用返回值?,javascript,angularjs,asynchronous,Javascript,Angularjs,Asynchronous,看这个视频, 您会发现代码中有一行写着: $scope.twitterResult = $scope.twitter.get({q:$scope.searchTerm}); 这是一个小怪癖: “twitter”的“get”方法显然是一个异步函数,它如何将值返回到$scope.twitterResult JSFIDLE(无法工作,因为twitter API已更改): 此代码$scope.twitter.get({q:$scope.searchTerm})返回延迟对象。角度视图的排列方式使视图在

看这个视频,

您会发现代码中有一行写着:

$scope.twitterResult = $scope.twitter.get({q:$scope.searchTerm});
这是一个小怪癖: “twitter”的“get”方法显然是一个异步函数,它如何将值返回到$scope.twitterResult

JSFIDLE(无法工作,因为twitter API已更改):


此代码
$scope.twitter.get({q:$scope.searchTerm})返回延迟对象。角度视图的排列方式使视图在延迟对象解析时更新。这只是一种方便

$scope.twitterResult = $scope.twitter.get({q:$scope.searchTerm});
您的代码仅适用于角度绑定

但如果您需要在运行时获取数据,则需要将此格式写在下面

如果
$scope.twitter
是一个URL,那么编写它

$http.get($scope.twitter, {q:$scope.searchTerm}).success(function (response) {
$scope.twitterResult=response;
}

$http必须在contoller

中定义:但是延迟对象如何知道应该更改哪个变量?或者延迟对象可以自行更改?当Angular在视图中遇到承诺时,它会自动设置一个成功回调,并在解析后用承诺替换结果值。在视图上直接使用Promissions是AngularJS的一项功能,许多人并不知道这一点,但它为我们的控制器尽可能精简提供了很多机会。例子: