ng-repeat声明中的数组问题

时间:2015-09-14 11:39:50

标签: javascript angularjs angularjs-ng-repeat

我的阵列:

$scope.pages = [{
    "id": "1",
    "names": ["Peter", "Rahul"]
}, {
    "id": "2",
    "names": ["Johnson", "Robert"]
}, ];

我的Html&角度

<div ng-repeat="page in pages">
     <a>{{page.id}}</a><br />
     <a ng-repeat="name in pages.names">{{page.name}}</a>
</div>

我只在输出中输入数字,但没有显示名称。

2 个答案:

答案 0 :(得分:2)

pages.names应为page.name

{{page.name}}应为{{name}}

变化:

<a ng-repeat="name in pages.names">{{page.name}}</a>

要:

<a ng-repeat="name in page.names">{{name}}</a>

答案 1 :(得分:1)

您应该使用<a ng-repeat="name in page.names">{{name}}</a>代替<a ng-repeat="name in pages.names">{{page.name}}</a>

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

function MyCtrl($scope) {
  $scope.pages = [{
    "id": "1",
    "names": ["Peter", "Rahul"]
  }, {
    "id": "2",
    "names": ["Johnson", "Robert"]
  }, ];
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="myApp">
  <div ng-controller="MyCtrl">
    <div ng-repeat="page in pages">
      <a>{{page.id}}</a>
      <br />
      <a ng-repeat="name in page.names">{{name}}</a>
    </div>
  </div>
</body>