我是AngularJS的新手,我在使用范围变量时遇到了一些麻烦。AngularJS范围变量undefined
下面是一个示例代码。我想知道为什么使用ng-repeat它显示$ scope.currencies的值,但是当我尝试从JS(console.log($ scope.currencies))访问时,它返回“undefined”
<!DOCTYPE html>
<html>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="appCtrl">
<ul>
<li ng-repeat="x in currencies">
{{ x }}
</li>
</ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('appCtrl', function($scope, $http) {
$http.get("http://localhost:8080/currencies")
.success(function (response) {$scope.currencies = response;});
console.log("currencies are "+$scope.currencies);
});
</script>
</body>
</html>
我觉得有什么东西我越来越错范围,任何人都可以给我一个线索?
您的console.log正在运行,然后$ http.get的响应可以触发.success方法。在那个时候,$ scope.currencies仍然是未定义的。 –
您对$ q/$ http/promise有错。 – dizel3d