我想将以下内容写入具有“items”字段的数据库:
$scope.variable = [[1,2],[3,4]];
项目的值 = 1,2,3,4 而不是 [1,2],[3,4]
当我将其写为 angular.toJson($scope.variable)
时,它确实会被分开,但是我如何将其 ng-repeat 为 1,2 和 3,4 而不是 [[1 ,2],[3,4]]
这不起作用:
<tr ng-repeat="x in dbValues"><td>{{x.items}}</td></tr>
最佳答案
要在 html 上填充二维数组,您需要执行 ng-repeat 两次
HTML
<tr ng-repeat="item in data track by $index">
<td ng-repeat="subItem in item">{{subItem}}</td>
</tr>
更新
对于像 { "_id": ObjectId("550ec51fd8b9bff112000006"), "items": "[[\"a\"],[\"a\",\"b\"]]"这样的 json ,“__v”:0}
您需要其中的项目数组,并且该项目包含在维度数组中,基本上您需要展平该数组。使用 Angular 自定义过滤器可以轻松实现
过滤
.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;
}
});
然后在标记上使用此过滤器来展平项目对象
标记
<tr ng-repeat="item in data track by $index">
<td ng-repeat="subItem in item.items | flattenarray track by $index">{{subItem}}</td>
</tr>
关于javascript - Angular mongodb数组格式写错了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29194514/