jonhobbs
jonhobbs

Reputation: 27952

ng-repeat needs to loop over child object of $scope

I've been led to believe that it's better to use a child object on a scope rather than adding straight to the scope... e.g.

$scope.model.mystuff

is better than

$scope.mystuff

However, my first very simple bit of code using ne-repeat works when you do this...

$scope.myStuff = [{},{},{}]

<div ng-repeat="things in myStuff">Test</div>

If I run that I see the word Test 3 times. If I do the following though...

$scope.model.myStuff = [{},{},{}]

<div ng-repeat="things in model.myStuff">Test</div>

Then it doesn't loop at all. I'm sure I've just misunderstood this and the solution is very simle.

Upvotes: 0

Views: 470

Answers (1)

Mathew Berg
Mathew Berg

Reputation: 28750

Next time, if you provide a jsfiddle, it might make things easier :)

The problem seems to be how you declared

$scope.model.myStuff = [{},{},{}]

$scope.model didn't exist at that time, so I just quickly added it the line before like so:

$scope.model = {}
$scope.model.myStuff = [{},{},{}]

And then the ng-repeat worked, outputting test 3 times

jsfiddle: http://jsfiddle.net/rtCP3/33/

Upvotes: 1

Related Questions