所以在观看了一些教程后,我创建了我的第一个测试应用程序,但由于某种原因它无法工作。 该应用程序的目标是从 json 文件中获取产品信息并将其放入 ng-repeat 中。
我的 HTML 代码: http://pastebin.com/fa9iLHim
我的 AngularJS 代码: http://pastebin.com/GJKzAwWZ
我的 JSON 数据: http://pastebin.com/yRXPzf7D
我的文件夹: /image/6tWE2.jpg
var app = angular.module('TrialerApp', []);
app.service('trialerProducts', function($http, $q) {
/*var deferred = $q.defer();
$http.get('/resources/json/data.json').then(function(data) {
deferred.resolve(data);
});*/
this.getProducts = function() {
/*return deferred.promise;*/ // stubbing response as we don't have access to backend
return $q.when([{
"name": "Crunchyroll",
"description": "A TV browsing thing"
}, {
"name": "Febreze",
"description": "Clean things up"
}, {
"name": "Minecraft",
"description": "A nice and fun game"
}, {
"name": "Door",
"description": "You'd better walk through this.."
}, {
"name": "Window",
"description": "An invisible wall!"
}])
}
});
app.controller('TrialerCtrl', function($scope, trialerProducts) {
var promise = trialerProducts.getProducts();
promise.then(function(response) {
$scope.players = response;
});
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<div class="mainContainer" ng-app="TrialerApp">
<h1>Trialer Products</h1>
<div ng-controller="TrialerCtrl">
<table class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="player in players">
<td>{{player.name}}</td>
<td>{{player.description}}</td>
</tr>
</tbody>
</table>
</div>
</div>
我认为链接我的文件等存在错误,但我不确定。
有人可以帮助我吗?
谢谢!
最佳答案
没有错误信息,很难说。
可能出现的问题:
HTML:此行缺少右大括号(对于某些浏览器来说不是问题,对于其他浏览器来说是个大问题,具体取决于):
KO <div class="mainContainer" ng-app="TrialerApp"
OK <div class="mainContainer" ng-app="TrialerApp">
Javascript:太多括号(对所有浏览器来说都是一个大问题)
KO $http.get('/resources/json/data.json').then(function (data))
OK $http.get('/resources/json/data.json').then(function (data)
关于javascript - AngularJS:我哪里错了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31314507/