使用AngularJS从{jason

时间:2015-10-14 06:00:18

标签: json angularjs node.js angularjs-ng-repeat getjson

我正在从头开始学习角度js,目前我正在尝试从json文件中检索数据。

我正在使用 - nodejs,express,AngularJS。

之前我在使用 -

时收到错误“Unexpected token D”
$http.get("/models/driversList.json").success(function(response){$scope.driversList = response})

已经解决但现在我用当前的代码得到了类似的东西 -

Drivers Championship Standings
1           

我猜测答案基本上是空白的,因此是“1”,但我不知道为什么会发生这种情况。

以下是我的文件 -

  

/app.js

app.use('/models',express.static(path.join(__dirname, 'private/data/db/models')));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/scripts',express.static(path.join(__dirname, 'public/javascripts')));
app.use('/styles',express.static(path.join(__dirname, 'public/stylesheets')));
  

/javascripts/app.js

angular.module('F1FeederApp', [
  'F1FeederApp.controllers'
]);
  

/javascripts/controllers.js

angular.module('F1FeederApp.controllers', []).
controller('driversController', function($scope, $http) {
  $http({
    method: 'GET',
    url: '/models/driversList.json',
    data: {},
    transformResponse: function (data, headersGetter, status) {
        //This was implemented since the REST service is returning a plain/text response
        //and angularJS $http module can't parse the response like that.
        return {data: data};
    }
}).success(function(response){ alert('worked'); $scope.driversList = response}).error(function(response) {
    $scope.driversList = [
      {
          Driver: {
              givenName: response,
              familyName: 'Vettel'
          },
          points: 322,
          nationality: "German",
          Constructors: [
              {name: "Red Bull"}
          ]
      },
      {
          Driver: {
          givenName: 'Fernando',
              familyName: 'Alonso'
          },
          points: 207,
          nationality: "Spanish",
          Constructors: [
              {name: "Ferrari"}
          ]
      }
    ];
})  
});
  

driversList.json

[
      {
          Driver: {
              givenName: 'Eldorado',
              familyName: 'Vettel'
          },
          points: 322,
          nationality: "German",
          Constructors: [
              {name: "Red Bull"}
          ]
      },
      {
          Driver: {
          givenName: 'Fernando',
              familyName: 'Alonso'
          },
          points: 207,
          nationality: "Spanish",
          Constructors: [
              {name: "Ferrari"}
          ]
      }
    ]
  

的index.html

<!DOCTYPE html>
<html>
<head>
  <title>F-1 Feeder</title>
  <script src="/scripts/angular.min.js"></script>
  <script src="/scripts/controllers.js"></script>
  <script src="/scripts/app.js"></script>
</head>

<body ng-app="F1FeederApp" ng-controller="driversController">
  <table>
    <thead>
      <tr><th colspan="4">Drivers Championship Standings</th></tr>
    </thead>
    <tbody> 
      <tr ng-repeat="driver in driversList">
        <td>{{$index + 1}}</td>
        <td>
          {{driver.Driver.givenName}}&nbsp;{{driver.Driver.familyName}}
        </td>
        <td>{{driver.Constructors[0].name}}</td>
        <td>{{driver.points}}</td>
      </tr>
    </tbody>
  </table>  
</body>
</html>

修改

@rattanak

要回答第3点,是的,如果我只是将数组变量赋值给范围变量,那么它是有效的。 error()中的部分是之前工作的部分。

我也尝试了第1点,似乎json出现在“data”中,所以我将response.data分配给driversList,现在我收到了这个错误 -

Data:
[{Driver:{givenName:'Eldorado',familyName:'Vettel'},points:322,nationality:"German",Constructors:[{name:"Red Bull"}]}]

Error: [ngRepeat:dupes] http://errors.angularjs.org/1.4.7/ngRepeat/dupes?p0=driver%20in%20driversList.data&p1=string%3Ar&p2=r
    at Error (native)
    at http://localhost:3000/scripts/angular.min.js:6:416
    at http://localhost:3000/scripts/angular.min.js:280:39
    at Object.fn (http://localhost:3000/scripts/angular.min.js:129:401)
    at n.a.$get.n.$digest (http://localhost:3000/scripts/angular.min.js:130:483)
    at n.a.$get.n.$apply (http://localhost:3000/scripts/angular.min.js:133:512)
    at h (http://localhost:3000/scripts/angular.min.js:87:376)
    at K (http://localhost:3000/scripts/angular.min.js:91:499)
    at XMLHttpRequest.z.onload (http://localhost:3000/scripts/angular.min.js:93:37)

表示存在重复

2 个答案:

答案 0 :(得分:0)

似乎$ http服务无法成功解析。没有实际测试代码很难分辨。使用Chrome Dev Console,我将通过以下方式调试代码:

  1. 在承诺的成功功能中使用console.log($scope.driversList)
  2. 在承诺的错误功能中使用console.log(error)

  3. 您是否可以在不使用$ http服务的情况下使代码生效,即只需将数据的数组变量分配给$scope.driversList

  4. 如果您能提供github链接或其他内容,我很乐意为您测试。

答案 1 :(得分:0)

您的文件driverlist.json不包含有效的JSON代码。此代码应为:

Function

纠正此问题,然后重试。

JSON始终使用双引号(&#34;)从不使用单引号(&#39;)

有关正确JSON的详细信息,请参阅http://www.json.org/