angularjs - 如何在 angularjs 中使用 ngFor

标签 angularjs

我是 Angular 的新手。我看到了构造函数、模板、选择器等。我不知道。我知道 $scope、$rootscope、模块和 Controller 。有了这些,我为 ngFor 编写了代码。我相信在 nfFor 之前,我们在 angular1 中有 ngRepeat。请更正。

 <html ng-app="moduleA">

 <head>
 <title> Collections

 </title>
 <script src="angular.js"></script>
   <link href="//cdnjs.cloudflare.com/ajax/libs/twitter-
bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
 <script 
 src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.20/angular.min.js">
 </script>
 <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-
bootstrap/3.2.0/js/bootstrap.min.js"></script>

</head>

 <body>
  <script>

   angular.module('moduleA')
  .controller('SubjectController',function($scope,$rootscope){

        $scope.todos = [
        { id: 1, title: 'Learn AngularJS', description: 'Learn,Live,Laugh AngularJS', done: true },
        { id: 2, title: 'Explore hibernate', description: 'Explore and use hibernate instead of jdbc', done: true },
        { id: 3, title: 'Play with spring', description: 'spring seems better have a look', done: false },
        { id: 4, title: 'Try struts', description: 'No more labour work..use struts', done: false },
        { id: 5, title: 'Try servlets', description: 'Aah..servlets stack seems cool..why dont u try once', done: false }
    ];
    });
</script>

<div ng-controller="moduleA">

        <table>
            <thead>
                <tr>
                    <th>#</th>
                    <th>Title</th>
                    <th>Description</th>
                    <th>Done?</th>
                </tr>
            </thead>
            <tbody>
                <tr ngFor="let todo of todos">
                <td> {{todo.id}}</td>
                <td> {{todo.title}}</td>
                <td> {{todo.description}}</td>
                <td> {{todo.done}}</td>
                </tr>
            </tbody>
        </table>

    </div>

最佳答案

不能在angularjs中使用ngFor,你可以使用 ng-repeat 遍历集合。

 <tr ng-repeat="todo in todos">

演示

  angular.module('moduleA',[])
  .controller('SubjectController',function($scope){

        $scope.todos = [
        { id: 1, title: 'Learn AngularJS', description: 'Learn,Live,Laugh AngularJS', done: true },
        { id: 2, title: 'Explore hibernate', description: 'Explore and use hibernate instead of jdbc', done: true },
        { id: 3, title: 'Play with spring', description: 'spring seems better have a look', done: false },
        { id: 4, title: 'Try struts', description: 'No more labour work..use struts', done: false },
        { id: 5, title: 'Try servlets', description: 'Aah..servlets stack seems cool..why dont u try once', done: false }
    ];
    });
<html ng-app="moduleA">

 <head>
 <title> Collections
 </title>
 <script src="angular.js"></script>
   <link href="//cdnjs.cloudflare.com/ajax/libs/twitter-
bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
 <script 
 src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.20/angular.min.js">
 </script>
 <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-
bootstrap/3.2.0/js/bootstrap.min.js"></script>
</head>
 <body>
  <script>

 
</script>

<div ng-controller="SubjectController">

        <table>
            <thead>
                <tr>
                    <th>#</th>
                    <th>Title</th>
                    <th>Description</th>
                    <th>Done?</th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="todo in todos">
                <td> {{todo.id}}</td>
                <td> {{todo.title}}</td>
                <td> {{todo.description}}</td>
                <td> {{todo.done}}</td>
                </tr>
            </tbody>
        </table>

    </div>

关于angularjs - 如何在 angularjs 中使用 ngFor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49418918/

相关文章:

javascript - HTML5 Input datetime-local default value of today and current time 使用AngularJs

javascript - API调用后的 Angular 回调

javascript - AngularJS 将一个数组放入 $cacheFactory 中

javascript - 如何使用 ssh2 和 node.js 计算远程服务器上的磁盘空间并存储值?

angularjs - ng-controller 位于 angularjs 指令内?

javascript - ng-show 和 localStorage 表现异常

javascript - Angular.js 缓存 $compiled 模板/ng-repeat 中指令的渲染性能

javascript - AngularJS 中的增量距离

javascript - Angular 加载数据 OnScroll

javascript - Angular JS 中的响应子节点应用于多维数组对象中的过滤器