1

I started learning AngularJS, but I am stuck on a problem. The ng-repeat is not looping through the users from the array. The list is just empty.

Here's my code.

app.js

var app = angular.module('myapp', []);

app.controller('dataCtrl', function ($scope) {
    $scope.users = [ 
        { name: 'Max', age: 20 },
        { name: 'Tom', age: 42 },
        { name: 'Alex', age: 41 },
        { name: 'Thomas', age: 3 },
        { name: 'Andreas', age: 17 },
        { name: 'Richard', age: 11 } 
    ]
});

index.html

<!DOCTYPE html>
<html ng-app="myapp" lang="de">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <title>Angular JS</title>
</head>

<body>
    <div class="container">
        <h1>Die Familie</h1>
        <table class="table" ng-controller="dataCtrl">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Age</th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="users in user">
                    <td>{{user.name}}</td>
                    <td>{{user.age}}</td>
                </tr>
            </tbody>
        </table>
    </div>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
    <script src="app.js"></script>
</body>
</html>

3 Answers 3

3

Your ngRepeat syntax is bass-ackwards - it's obj in collection not collection in obj

ng-repeat="user in users">
Sign up to request clarification or add additional context in comments.

1 Comment

By the time I realized the issue, answer was there... Too quick...+1
1

Yes, the other answers above are why. May I suggest since you are learning you look into the "controller as" functionality and get rid of $scope. Anytime you learn something they show you the wrong way first.

Comments

1

You have a typo ... it should be user in users... see plunker

   <tr ng-repeat="user in users">
                    <td>{{user.name}}</td>
                    <td>{{user.age}}</td>
                </tr>

As a follow-up to the other comment, one recommend way is to use 'var vm=this' instead of $scope in your controller and 'controller as syntax' in your HTML. I updated the plunker. Your HTML would look like:

  <body ng-controller="MainCtrl as vm">
    <p>Hello {{vm.name}}!</p>

    <div class="container">
        <h1>Die Familie</h1>
        <table class="table">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Age</th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="user in vm.users">
                    <td>{{user.name}}</td>
                    <td>{{user.age}}</td>
                </tr>
            </tbody>
        </table>
    </div>
  </body>

Note that users become vm.users.

And your app:

var app = angular.module('plunker', []);

app.controller('MainCtrl', function() {
  var vm = this;
  vm.name = 'World';

   vm.users = [ 
        { name: 'Max', age: 20 },
        { name: 'Tom', age: 42 },
        { name: 'Alex', age: 41 },
        { name: 'Thomas', age: 3 },
        { name: 'Andreas', age: 17 },
        { name: 'Richard', age: 11 } 
    ];
});

Using both of those techniques avoid a lot of $scope issues as your code grow... especially with callbacks, functions, etc. where you end up with $scope meaning different things and it is a pain to debug. Here is an article giving more info: "AngularJS's Controller As and the vm Variable" from John Papa and his Angular style guide is good to read too.

Comments

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.