AngularJS ng-repeat不列出数据

时间:2016-07-01 18:34:36

标签: javascript angularjs

我开始学习AngularJS,但在第一次编码时遇到了问题。

ng-repeat没有在表格中列出我的用户。

如下所示,一切都是正确的,控制台不打印任何内容。

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 个答案:

答案 0 :(得分:3)

您的ngRepeat语法是低音 - 它是obj in collection而不是collection in obj

ng-repeat="user in users">

答案 1 :(得分:1)

你有一个错字...它应该是用户的用户...请参阅plunker

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

作为对其他评论的后续跟进,一种推荐的方法是在控制器中使用'var vm = this'而不是$ scope,在HTML中使用'controller as syntax'。我更新了plunker。 你的HTML看起来像:

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

请注意,用户将成为vm.users。

和你的应用:

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

使用这两种技术可以避免在代码增长时出现很多$ scope问题...尤其是回调,函数等等,最终你的$ scope意味着不同的东西,调试很麻烦。这是article giving more info: "AngularJS's Controller As and the vm Variable" from John Papa和他的Angular style guide is good to read too.

答案 2 :(得分:1)

是的,上面的其他答案就是原因。我可以建议,因为你正在学习,你会看到“控制器为”功能并摆脱$ scope。每当你学到东西时,他们首先会向你显示错误的方法。