hwg*_*hwg 3 javascript angularjs angularjs-scope angularjs-controller angularjs-http
我尝试将$ http.get中的数据分配给我的控制器中的变量.
$http.get(URL).success(function (data) {
$scope.results = data;
console.log('results in $http.get :'+ $scope.results);
});
console.log('results after http.get'+ $scope.results);
Run Code Online (Sandbox Code Playgroud)
第一个控制台日志打印数据来自get.在$ http.get(url).success $ scope.results打印为undefined之后.
这是因为$http.get
是异步的.因此,在ajax请求完成之前,您的代码不会被搁置,而是执行其余的代码.所以你的第二个console.log
将在ajax请求完成之前执行.此时,没有调用的范围变量$scope.results
,仅在请求完成后定义,这就是它打印的原因undefined
.您的第一个console.log
将仅在$http
ajax成功完成后打印,此时您已$scope.results
分配data
来自后端.
归档时间: |
|
查看次数: |
7206 次 |
最近记录: |