【发布时间】:2016-12-13 12:59:21
【问题描述】:
我是 Angular 的新手,我正在经历这个 tutorial 并且我遇到了问题 解析给定的 json,如下所示:
{ "records": [
{
"Name" : "Alfreds Futterkiste",
"City" : "Berlin",
"Country" : "Germany"
},
{
"Name" : "Centro comercial Moctezuma",
"City" : "México D.F.",
"Country" : "Mexico" },
{
"Name" : "Ernst Handel",
"City" : "Graz",
"Country" : "Austria" },
{
"Name" : "FISSA Fabrica Inter. Salchichas S.A.",
"City" : "Madrid",
"Country" : "Spain" },
{
"Name" : "Island Trading",
"City" : "Cowes",
"Country" : "UK"
}
] }
问题是 json 被返回为“未定义”并且没有显示在页面上 一点也不。这是 index.html:
<!DOCTYPE html>
<html ng-app="MyApp">
<head>
<title>AngularJS Tutorial Series</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
</head>
<body>
<div ng-controller="HelloController">Hi {{name}}, welcome to AngularJS Tutorial Series</div>
<div ng-controller="AboutController">Brought to you by {{name}}.</div>
<h2>Load me some JSON data : )</h2>
<table ng-controller="HelloController">
<tr>
<th>Name</th>
<th>City</th>
<th>Country</th>
</tr>
<tr ng-repeat="country in countries">
<td>{{country.Name}}</td>
<td>{{country.City}}</td>
<td>{{country.Country}}</td>
</tr>
</table>
<!-- Angular JS Scripts -->
<script src="js/angular.min.js"></script>
<script src="js/angular-route.min.js"></script>
<!-- AngularJS Application Specific Scripts -->
<script src="app/app.js"></script>
<script src="app/controllers/homeController.js"></script>
<script src="app/controllers/aboutController.js"></script>
</body>
</html>
这是 homeController.js 文件:
MyApp.controller('HelloController', hello);
function hello($scope, $http){
$scope.name = "Rodrick";
$http.get('countries.json').then(function(data) {
$scope.countries = data.records;
});
}
使用 Google Chrome 开发者工具调试给了我一个警告,上面写着:
为已经承载的元素调用 Element.createShadowRoot() 不推荐使用影子根。看 https://www.chromestatus.com/features/4668884095336448 了解更多 详情。
ng-inspector 在“HelloController”下列出未定义的国家/地区。
为了完整起见,这里是app.js和aboutController.js:
app.js:
var MyApp = angular.module("MyApp", []);
关于Controller.js:
MyApp.controller('AboutController', about);
function about($scope)
{
$scope.name = "Kode Blog Tutorials";
}
任何帮助将不胜感激,并提前感谢您。
【问题讨论】: