2015-06-14 36 views
0


我試圖訪問對象的數組在我的控制器:
AngularJS - 訪問對象的數組中的一個控制器發生故障

angular.module('schoolManagement').controller('CycleController', ['$scope', function($scope){ 
    $scope.cycles = [ 
     { 
      nomCycle : 'Primaire', 
      active : 'oui' 
     }, 
     { 
      nomCycle : 'Collège', 
      active : 'non' 
     } 
    ]; 
    console.log($scope.cycles[0].nomCycle); 
}]); 

的console.log()給正是我要找的控制檯,但是當我在我看來,在陣列上使用NG-重複循環,這是行不通的:

<tbody ng-controller="CycleController as CycleCtrl"> 
    <tr ng-repeat="cycle in CycleCtrl.cycles"> 
     <td> 
      <input type="checkbox" /> 
     </td> 
     <td>{{cycle.nomCycle}}</td> 
     <td>{{cycle.active}}</td> 
    </tr> 
</tbody> 


編輯:
由於我使用$scope也沒有必要使用控制器的語法,在這個正確的形式:

<tr ng-repeat="cycle in cycles">...</tr> 
+0

您正在使用控制器別名語法,並嘗試從控制器實例獲取屬性時,它被分配給範圍屬性。 – PSL

回答

1

我認爲線

<tr ng-repeat="cycle in CycleCtrl.cycles"> 

談情是

<tr ng-repeat="cycle in cycles"> 

PS:在法國,Primère是拼寫編輯:PRIMAIRE

+1

謝謝!你是P.S.做了我的一天,哈哈,這是多麼愚蠢的錯誤。 thx隊友! – bboulahdid

2

如果使用controller as語法,您需要將控制器更改爲:

.controller('CycleController', [function() { 
    this.cycles = [{ 
    nomCycle: 'Primère', 
    active: 'oui' 
    }, { 
    nomCycle: 'Collége', 
    active: 'non' 
    }]; 
}]); 

你不需要$scope這裏。如果你需要觀察者等等的東西,你可以再次添加它。

1

你不需要用控制器的名字限定數組,因爲它在範圍內。相反,只是:

<tr ng-repeat="cycle in cycles"> 
+0

是的,你說得對。 thx男人! – bboulahdid

+0

@BOB:沒問題! aorfevre在我的面前張貼了他的答案。你應該把它標記爲正確的。 –

相關問題