AngularJS:我哪里错了?

时间:2015-07-09 10:14:38

标签: javascript html json angularjs

所以在看了一些教程之后我创建了我的第一个测试应用程序,但由于某种原因它无法正常工作。 该应用程序的目标是从json文件中获取产品信息并将其放入ng-repeat。

我的HTML代码: http://pastebin.com/fa9iLHim

我的AngularJS代码: http://pastebin.com/GJKzAwWZ

我的JSON数据: http://pastebin.com/yRXPzf7D

我的文件夹: http://imgur.com/CPd2egG



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>
&#13;
&#13;
&#13;

我认为在链接我的文件等方面存在错误,但我不确定。

任何人都可以帮助我吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

没有错误信息,很难说。

可能出现的问题:

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)