2016-11-17 58 views
-2

我在解决ng-repeat指令时遇到了问题,因为我是初学者,无法自行解决它。无法迭代

var myModule = angular.module("myFirst",[]); 
 

 
myModule.controller("cont", function($scope){ 
 
var employees = [ 
 
{name: "Manju", age :"23", rank:"2"}, 
 
{name: "SivaSankari", age :"23", rank:"1"}, 
 
{name: "Gayathri", age :"23", rank:"1"}, 
 
]; 
 
$scope.employees = employees; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<html ng-app="myFirst"> 
 
    <head> 
 
    <title> My workout|ngRepeat 
 
    </head> 
 
<body> 
 
<div ng-controller="cont"> 
 
<table> 
 
<thead> 
 
<th>Name</th> 
 
<th>Age</th> 
 
<th>Postions</th> 
 
</thead> 
 
<tbody> 
 
<tr ng-repeat="x in employees"> 
 
<td>{{employees.name}}</td> 
 
<td>{{employees.age}}</td> 
 
<td>{{employees.position}}</td> 
 
</tr> 
 
</tbody> 
 
</table> 
 

 
</div> 
 

 
</body> 
 
    <html>

我也有怀疑是X键。可以给任何关键值吗? 任何人都可以帮助我解决和理解? 谢谢。

+0

你应该写为'​​{{x.name}}''不是员工。姓名' –

回答

0

首先

你必须使用x而不是employees因为employees是你的阵列和x当前项目

您正试图访问不存在的财产position在你的对象旁边。你应该叫rank

var myModule = angular.module("myFirst", []); 
 

 
myModule.controller("cont", function($scope) { 
 
    var employees = [{ 
 
    name: "Manju", 
 
    age: "23", 
 
    rank: "2" 
 
    }, { 
 
    name: "SivaSankari", 
 
    age: "23", 
 
    rank: "1" 
 
    }, { 
 
    name: "Gayathri", 
 
    age: "23", 
 
    rank: "1" 
 
    }, ]; 
 
    $scope.employees = employees; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<html ng-app="myFirst"> 
 

 
<body> 
 
    <div ng-controller="cont"> 
 
    <table> 
 
     <thead> 
 
     <th>Name</th> 
 
     <th>Age</th> 
 
     <th>Postions</th> 
 
     </thead> 
 
     <tbody> 
 
     <tr ng-repeat="x in employees"> 
 
      <td>{{x.name}}</td> 
 
      <td>{{x.age}}</td> 
 
      <td>{{x.rank}}</td> 
 
     </tr> 
 
     </tbody> 
 
    </table> 
 

 
    </div> 
 

 
</body> 
 
<html>

+0

嗨,是我改变了代码,错误地粘贴在这里。谢谢。 –

0

应该

<td>{{x.name}}</td> 
<td>{{x.age}}</td> 
<td>{{x.position}}</td> 
0

你应该访问的x不是employee阵列性能

<td>{{x.name}}</td> 
<td>{{x.age}}</td> 
<td>{{x.position}}</td> 
0

你定义x作为你遍历员工。因此,重复的部分应该是:

<tr ng-repeat="x in employees"> 
<td>{{x.name}}</td> 
<td>{{x.age}}</td> 
<td>{{x.position}}</td> 
</tr> 
0

当你使用ng-repeat="x in employees"这意味着你是说在每个迭代的项目分配给x,这就是为什么你不能任何其他变量以外x使用。

<td>{{x.name}}</td> 
<td>{{x.age}}</td> 
<td>{{x.position}}</td> 
0

我不明白你在标题标签中的ngRepeat,但是你的表声明没什么意义。

<tr ng-repeat="x in employees"> 

意味着,对于每一个元素阵列employees在(今后将被作为x称为),重复此元件即表行。 (请确保这就是你真正想做的事情)

因此,您的td里面,你得写 -

<td>{{x.name}}</td> 

我也有怀疑是X键。可以给任何关键值吗?

如果您的意思是说您是否可以拨打x其他任何东西,那么您可以。 例如

<tr ng-repeat="singleEmployee in employees"> 
<td>{{singleEmployee.name}}</td> 
+0

嗨,你说的标题标签,你不明白,对不起,我认为我在一个简单的工作中得到一个问题在ng重复,所以认为,可以保持这样的标题。我已经检查了其他问题,然后只保留。感谢你的回答.. –

0

你有NG重复的问题,它应该是x.name不employess.name

<tr ng-repeat="x in employees"> 
    <td>{{x.name}}</td> 
    <td>{{x.age}}</td> 
    <td>{{x.position}}</td> 
    </tr>