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 );
    – Anita
    CommentedApr 10, 2015 at 12:55
  • @Anita i tried. in such case i see "{}" inside divCommentedApr 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
    – Anita
    CommentedApr 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']} 
5
  • It work once, then error occurs ngRepeat:dupes. Anyhow i need to add empty element.CommentedApr 10, 2015 at 13:08
  • 1
    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 exactlyCommentedApr 10, 2015 at 13:09
  • i want to change row.cell array after each addColumn trigger. to have {"id": 10, "cell": ['a','b','c','','','','']}CommentedApr 10, 2015 at 13:12
  • WHat I mean is I don't know how you are setting values. Try using track byCommentedApr 10, 2015 at 13:13
  • 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
    – Anita
    CommentedApr 10, 2015 at 13:56
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
  • in such case new array element value equal '{}'CommentedApr 10, 2015 at 13:10

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.