Angularjs ng-repeat值打印问题

时间:2016-12-29 11:16:06

标签: javascript angularjs

我在角度控制器中有一个带有对象数组的范围变量调用项。它采用以下格式,这就是它在chrome控制台中显示的方式,

data
Array[4]
0
:
Object
created_at
:
"2016-12-29 08:20:21"
id
:
1
name
:
"Developer questions"
question_classification_type_id
:
1
status
:
1
updated_at
:
"2016-12-29 08:20:21"

所以这是我在html中的ng-repeat指令。

<span ng-repeat="result in items track by $index">
<p>{{result.name}}</p>
<p>{{result.description}}</p>
</span>

但它没有在视图中显示任何内容。我无法弄清楚原因,因为我之前做过类似的事情没有任何问题。所以请帮助别人来解决这个问题。

1 个答案:

答案 0 :(得分:0)

result.description您提供的items数组中没有描述,如果您不发布一些控制器代码,我们会给出下面的示例,并将其与此进行比较并尝试确定您的遗失。我的建议是从服务或api分配数据后控制$ scope.items并检查我提供的格式....有时它可能看起来像res.data或data.data .....

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.items = [
    {
      "created_at":"2016-12-29 08:20:21",
      "id":1,
      "name":"Developer questions",
      "question_classification_type_id":1,
      "status":1,
      "updated_at":"2016-12-29 08:20:21"
      
    },
 {
      "created_at":"2016-12-30 08:20:21",
      "id":2,
      "name":"Developer answers",
      "question_classification_type_id":1,
      "status":1,
      "updated_at":"2016-12-30 08:20:21"
      
    },
]
});
<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
    <pre>{{items}}</pre>
<span ng-repeat="result in items track by $index">
<p>{{result.name}}</p>
<p>{{result.description}}</p>
</span>
  </body>

</html>