0

I want to write the following to my db that has a field 'items':

$scope.variable = [[1,2],[3,4]];

value of items = 1,2,3,4 instead of [1,2],[3,4]

When I write it as angular.toJson($scope.variable) it does get separated, but how do I ng-repeat it as 1,2 and 3,4 instead of [[1,2],[3,4]]

This doesn't work :

<tr ng-repeat="x in dbValues"><td>{{x.items}}</td></tr>
4
  • <tr ng-repeat="item in x"><td>{{item}}</td></tr> Commented Mar 22, 2015 at 12:54
  • it should not be {{x.items}}, it would be {{x}} only Commented Mar 22, 2015 at 12:56
  • 1
    I edited question so to make it easier to understand Commented Mar 22, 2015 at 13:01
  • @brucelee did you checked my answer? Does i missed something? Commented Mar 22, 2015 at 13:04

1 Answer 1

2

For getting populated two dimentional array on html you need to do ng-repeat twice

HTML

<tr ng-repeat="item in data track by $index">
  <td ng-repeat="subItem in item">{{subItem}}</td>
</tr>

Working Plunkr here

Update

For json like { "_id" : ObjectId("550ec51fd8b9bff112000006"), "items" : "[[\"a\"],[\"a\",\"b\"]]", "__v" : 0 }

You need items array from it, and inside that items are contained in dimentional array, basically you need to flatten that array. And it can be easily possible using angular custom filter

Filter

.filter('flattenarray', function(){
  return function(valuesString){
    var jsonValues = angular.fromJson(valuesString), returnValue = [];
    angular.forEach(jsonValues, function(value, index){
      angular.forEach(value, function(v, i){
        returnValue.push(v);
      });
    });
    return returnValue;
  }
});

Then use this filter on markup to flatten items object

Markup

<tr ng-repeat="item in data track by $index">
  <td ng-repeat="subItem in item.items | flattenarray track by $index">{{subItem}}</td>
</tr>

Updated Plunkr

Sign up to request clarification or add additional context in comments.

8 Comments

It's not working :( I did this now: <tr ng-repeat="x in dbValue track by $index"><td><span ng-repeat="content in x.items track by $index">{{content}}</span></td></tr>
Does x(each object) contains items object? I think x.items should be only x, your code should be <tr ng-repeat="x in dbValue track by $index"><td><span ng-repeat="content in x track by $index">{{content}}</span></td></tr>
yes it contains item object. result is [[1,2],[3,4]] if I do content in x I jus get more stuff like the id etc. the .items part contains the [[1,2],[3,4]] value
could you please provide me the whole json? I'm not able to get whole picture what you are talking..
thank you very much, much appreciated for your time and effort thanks man
|

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.