这是嵌套数组的结构:
$scope.History = [
{
isCustomer:false,
userText:"some text",
options:[]
}
包含数据的数组: // DisplayLabel中的文本对于所有对象都是相同的。
$scope.Categories = [
{ "QuestionId": 1, "QuestionName": "Complaint", "DisplayLabel": "what is it?" },
{ "QuestionId": 2, "QuestionName": "Registration", "DisplayLabel": "what is it?" }
];
所需数组:
$scope.History = [
{
isCustomer:false,
userText:"what is it?",
options:["Complaint","Registration"]
}
通常我会为1D数组执行此操作
angular.forEach($scope.Categories, function (value, key) {
$scope.History.push(false,value.DisplayLabel);
现在,如何将商品添加到'选项'对象。
修改 我试过这个,但没有运气。不显示任何数据
var optionsData = [];
var userText = "";
angular.forEach($scope.Categories, function (value, key) {
optionsData.push(value.QuestionName);
userText = value.DisplayLabel;
})
$scope.History.push(false,userText,optionsData);
答案 0 :(得分:0)
var app = angular.module('app', []);
app.controller('homeCtrl', function($scope) {
$scope.History = [{
isCustomer: false,
userText: "some text",
options: []
}]
$scope.Categories = [{
"QuestionId": 1,
"QuestionName": "Complaint",
"DisplayLabel": "what is it?"
}, {
"QuestionId": 2,
"QuestionName": "Registration",
"DisplayLabel": "what is it?"
}];
var optionsData = []
angular.forEach($scope.Categories, function(value, key) {
$scope.History[0].options.push(value.QuestionName)
});
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<div ng-app="app">
<div ng-controller="homeCtrl">
<pre>{{History | json}}</pre>
</div>
</div>