Javascript 无法使用angularjs读取json
在下面的代码中Javascript 无法使用angularjs读取json,javascript,angularjs,json,Javascript,Angularjs,Json,在下面的代码中 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Dashboard</title> <!-- Libs --> <script src="angular.js"></script> <script>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Dashboard</title>
<!-- Libs -->
<script src="angular.js"></script>
<script>
var countryApp = angular.module('countryApp', []);
countryApp.controller('CountryCtrl', function ($scope, $http){
$http.get('country_codes.json').success(function(data) {
$scope.countries = data;
});
});
</script>
</head>
<body ng-controller="CountryCtrl">
</body>
</html>
根据调试,我没有看到任何错误,回调函数也没有执行 为什么控制器回调不执行?在html标记中添加ng app=“countryApp”后,它对我有效。 在html标签中添加ng app=“countryApp”后,它对我有效。
添加错误功能,您将看到错误详细信息:
<script>
var countryApp = angular.module('countryApp', []);
countryApp.controller('CountryCtrl', function ($scope, $http){
$http.get('country_codes.json')
.then(function(data) {$scope.countries = data;}
,
function(data) {console.log("error: "+data)});
});
</script>
添加错误函数,您将看到错误详细信息:
<script>
var countryApp = angular.module('countryApp', []);
countryApp.controller('CountryCtrl', function ($scope, $http){
$http.get('country_codes.json')
.then(function(data) {$scope.countries = data;}
,
function(data) {console.log("error: "+data)});
});
</script>
您还应该处理
.error
回调,而不仅仅是.success
。您还应该处理.error
回调,而不仅仅是.success
。是的,ng应用程序告诉angularjs HTML标记元素是angularjs应用程序的“所有者”。您可以将ng app添加到任何标记(如div标记),但代码仍然不起作用。你能修改代码吗?是的,ng应用程序告诉angularjs HTML标记元素是angularjs应用程序的“所有者”。您可以将ng app添加到任何标记(如div标记),但代码仍然不起作用。你能修改密码吗?
{
"countries": [
{
"code": "AD",
"name": "Andorra",
"population": "84000"
},
{
"code": "AE",
"name": "United Arab Emirates",
"population": "4975593"
}
]
}