0


I'm trying to access an array of objects in my controller :

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);
}]);

The console.log() gives exactly what i'm looking for the in console, but when i use ng-repeat to loop over the array in my view, it doesn't work :

<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>


EDIT :
Since i'm using $scope there is no need to use the controller syntax, the correct form in this :

<tr ng-repeat="cycle in cycles">...</tr>
1
  • You are using controller alias syntax and trying to get property from controller instance when it is assigned to scope property. Commented Jun 14, 2015 at 16:43

3 Answers 3

2

If you use the controller as syntax, you need to change your controller to:

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

You don't need the $scope here. You may add it again, if you need things like watchers, etc.

Sign up to request clarification or add additional context in comments.

Comments

1

I assume that line

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

Shall be

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

P.S: In french, Primère is spelled : PRIMAIRE

1 Comment

Thank you! You're P.S. made my day, hhah, what a stupid mistake. thx mate!
1

You don't need to qualify the array with the name of the controller, since it's in scope. Instead, just:

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

1 Comment

@BOB: No problem! aorfevre posted his answer before mine. You should mark it as correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.