1

Both the ng-repeat's are not working. But just showing {{ rows }} does work. So the communication between the template and the controller is working.

What's wrong with my ng-repeat?

I already searched the other posts on this topic, but on all of them the error was in the controller. But even repeating an I in an array defined in the HTML template isn't working. And the Rows are showing up when not looping.

<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++;
  }
});
0

3 Answers 3

1

Both are working fine, just use the (key, value) separator on your index based arrays.

<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>
Sign up to request clarification or add additional context in comments.

Comments

1

Try using track by For example:

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

Working Demo

Comments

1
  1. Problem with your html - you are not using td inside tr

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

  2. you have duplicate values in the list - use track by $index in ng-repeat

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

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

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.