繁体   English   中英

ng-repeat不工作AngularJS

[英]ng-repeat not working AngularJS

两个ng-repeat都不起作用。 但只显示{{rows}}确实有效。 因此模板和控制器之间的通信正在起作用。

我的ng-repeat什么问题?

我已经搜索了关于这个主题的其他帖子,但是在所有这些帖子中,错误都在控制器中。 但即使重复HTML模板中定义的数组中的I也无效。 并且当不循环时,行显示出来。

<div ng-controller="OrderNewCtrl">

  <h1> {{ rows }}</h1>
  <div ng-repeat="i in [42, 42, 43, 43]">
    {{ i }}
    <p>test</p>
  </div>

  <div class="form">
    <form>
      <table>
        <tbody>
          <tr ng-repeat="rowContent in rows">
            {{rowContent}}
          </tr>
        </tbody>
      </table>
      <button class="btn btn-primary btn-functionality btn-success btn-add" ng-click="addRow">+</button>
    </form>
  </div>
</div>

'use strict';

angular.module('myApp.orderNew', ['ngRoute'])

.config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/order/new', {
    templateUrl: 'order-new/order-new.template.html',
    controller: 'OrderNewCtrl'
  });
}])

.controller('OrderNewCtrl', function($scope) {
  $scope.rows = ['Row 1', 'Row 2', 'Row 3', 'Row 4'];
  $scope.counter = 4;
  $scope.test = "hi";

  $scope.addRow = function() {
    $scope.rows.push('Row ' + $scope.counter);
    $scope.counter++;
  }
});

两者都工作正常,只需在基于索引的数组上使用(key, value)分隔符。

<div ng-controller="OrderNewCtrl">
  <h1> {{ rows }}</h1>
  <div ng-repeat="(key, value) in [42, 42, 43, 43]">
    {{ key }}
    <p>test</p>
  </div>
  <div class="form">
    <form>
      <table>
        <tbody>
          <tr ng-repeat="(rowContent, value) in rows">
            {{rowContent}}
          </tr>
        </tbody>
      </table>
      <button class="btn btn-primary btn-functionality btn-success btn-add" ng-click="addRow">+</button>
    </form>
  </div>
</div>

尝试使用track by例如:

<div ng-repeat="n in ['Row 1', 'Row 2', 'Row 3', 'Row 4'] track by $index">
    {{n}}
</div>

工作演示

  1. 你的HTML问题 - 你没有在tr里面使用td

    <tr ng-repeat="rowContent in rows track by $index"> <td>{{rowContent}}</td> </tr>

  2. 您在列表中有重复值 - 在ng-repeat使用track by $index

    ng-repeat="i in [42, 42, 43, 43] track by $index"

    ng-repeat="rowContent in rows track by $index"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM