我的ng-repeat不起作用

时间:2015-10-02 23:32:18

标签: angularjs angularjs-ng-repeat

当我生成一个ng-repeat时,我遇到了一些问题,html中没有任何内容。

有人可以帮我解决这个问题吗?

var myApp = angular.module('myApp', [])
.controller('MyCtrl', function($scope){
    list = [];

    $scope.generateAlbum= function() {
        return {
            Name : faker.name.firstName()       
        };
    };

    $scope.generate= function(count) {
        count = count || 25;
        list;
        for (var i = 0; i < count; i++) {
            list.push($scope.generateAlbum());
        }
        return list;
    };

    $scope.generate();

});

这里是html:

<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
    <script src="http://marak.com/faker.js/js/faker.js"></script>
    <script src="js/app.js" type="text/javascript"></script>
</head>
<body>
<main>
    <section ng-controller="MyCtrl">
        <ul ng-repeat="item in list">
            <li>{{item.Name}}</li>
        </ul>
    </section>
</main>

</body>
</html>

以及与示例的链接: http://plnkr.co/edit/cjm53O4ne8r929jiJoeR?p=preview

1 个答案:

答案 0 :(得分:1)

您的列表需要是范围属性。你的功能不是

var myApp = angular.module('myApp', [])
.controller('MyCtrl', function($scope){
  var list = [];

  function generateAlbum() {
        return {
            Name : faker.name.firstName()       
        };
  }

  function generate(count) {
        count = count || 25;
        for (var i = 0; i < count; i++) {
            list.push(generateAlbum());
        }
        $scope.list = list;
    }

    generate();
});

http://plnkr.co/edit/iaNWY9WTt5JYB9oIjAqu?p=preview