Angularjs ng-repeat inside ng-repeat, internal array cannot update

I came from China, my English is very poor, so I demo . Can I update the array inside ng-repeat?

+4
source share
1 answer

HTML:

<body ng-app="main" ng-controller="DemoCtrl">

  <table ng-table class="table">
    <tr ng-repeat="user in users">
      <td data-title="'Name'">{{user.name}}</td>
      <td data-title="'Age'">{{user.age}}</td>
      <td>
        {{user.spms|json}}
        <div ng-repeat="u in user.spms">
          <span ng-bind="u"></span>
          <input type="text" ng-model="u" ng-change='updateArray($parent.$index, $index, u)'>
        </div>
      </td>
    </tr>
  </table>

</body>

JS:

var app = angular.module('main', []).
controller('DemoCtrl', function($scope) {

  $scope.users = [{
      name: "Moroni",
      age: 50,
      spms: [
        6135.7678, 
        504.4589,
        2879.164, 
        669.7447
        ]
    },
    {
      name: "seven",
      age: 30,
      spms: [
        34135.7678, 
        5034.42589,
        22879.1264, 
        63469.72447
        ]
    }

  ];

  $scope.updateArray = function(parent, index, u) {
    $scope.users[parent].spms[index] = u * 1; // multiply by one to keep the value a Number
  }

})

Each update has problems associated with changing the scope, so you can change only once, then click and then change - so I would recommend adding a button for update values ​​and implementing more or less the same logic to update the array values .

Demo

0
source

Source: https://habr.com/ru/post/1546255/


All Articles