2016-11-17 56 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>