AngularJS - 访问控制器中的对象数组出错

AngularJS - accessing a array of objects in a controller goes wrong


我正在尝试访问控制器中的对象数组:

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>

我假设那行

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

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

P.S:在法语中,Primère 拼写为:PRIMAIRE

如果您使用 controller as 语法,您需要将控制器更改为:

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

这里不需要$scope。需要watcher之类的可以再添加

您不需要用控制器的名称限定数组,因为它在范围内。相反,只是:

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