user947668
user947668

Reputation: 2718

angularjs push empty element to array

I'm trying to add new rows.cell element but push syntax is not clear for me. If i specify push([]) i see on page changes. However instead of new empty div created i see new with square brackets "[]".

Pleas advise how to add new empty element to each rows.cell array?

html code:

        <div class="row" ng-repeat="row in rows">
            <div class="cell" ng-repeat="cell in row.cell">{{ cell }}</div>
        </div>
      </div>

controller code:

     angular.module("myapp", [])
     .controller("infoTable", function($scope) {
        $scope.rows = [
                       {"id": 10, "cell": ['a','b','c']}, 
                       {"id": 11, "cell": ['a','b','c']}
                      ];
        $scope.addColumn = function (){
            $scope.rows.forEach(function($row){
                $row.cell.push([]);
            });
        }
     });

Solved: "track by $index" added to ng-repeat="cell in row.cell" And now new array element can be added using

$row.cell.push('');

Upvotes: 0

Views: 3793

Answers (2)

Stephen Punwasi
Stephen Punwasi

Reputation: 476

You're pushing an array to the array. I'm not positive, but I think you're looking to push a document to the array. Try

$row.cell.push({}); 

Upvotes: 1

charlietfl
charlietfl

Reputation: 171679

If you wanted to add 'd' to each array it would be

$scope.rows.forEach(function($row){
   $row.cell.push('d');
});

//{"id": 10, "cell": ['a','b','c','d']}

Upvotes: 2

Related Questions