0

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('');
3
  • 1
    try var cell={}; $row.cell.push(cell ); Commented Apr 10, 2015 at 12:55
  • @Anita i tried. in such case i see "{}" inside div Commented Apr 10, 2015 at 13:06
  • if you want then you can put condition like this : '<div class="cell" ng-repeat="cell in row.cell"> <span ng-show="cell !== undefined && cell !== null ">{{ cell }}</span></div>' That may help you Commented Apr 10, 2015 at 13:16

2 Answers 2

2

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

5 Comments

It work once, then error occurs ngRepeat:dupes. Anyhow i need to add empty element.
Well you would need to change the value each time or use track by $index in your ng-repeat. Not really sure what you are trying to do exactly
i want to change row.cell array after each addColumn trigger. to have {"id": 10, "cell": ['a','b','c','','','','']}
WHat I mean is I don't know how you are setting values. Try using track by
This code will add 'd' element to cell. Show in ng-repeat it will show 'd' in the div.. How this will create empty div? Your question was to create empty div. Please guide me
1

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({}); 

1 Comment

in such case new array element value equal '{}'

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.