【发布时间】:2019-11-05 19:05:01
【问题描述】:
我有一些复选框,其值来自使用 ngFor 的 json。当我选择这些复选框并单击提交时,我需要以代码部分输出中提到的对象数组的形式捕获“li”标记值和选定的复选框值。在这里,我只得到数组中的“li”标签值/文本,但我没有得到如何将它与选定的复选框值(如输出格式)一起推送到对象中。这是下面的代码。
home.component.html
<div class="col-md-3" id="leftNavBar">
<ul *ngFor="let item of nestedjson">
<li class="parentNav">{{item.name}}</li>
<li class="childData">
<ul>
<li *ngFor="let child of item.value">{{child}}<span class="pull-right"><input type="checkbox"></span></li>
</ul>
</li>
</ul>
<div><button type="submit" (click)="getit()">submit</button></div>
</div>
home.component.ts
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import Speech from 'speak-tts';
import { RxSpeechRecognitionService, resultList, } from '@kamiazya/ngx-speech-recognition';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css'],
providers: [ RxSpeechRecognitionService ]
})
export class HomeComponent implements OnInit {
data:any;
nestedjson:any;
message = '';
test:any;
constructor(private formBuilder: FormBuilder,public service: RxSpeechRecognitionService) {
}
ngOnInit() {
this.nestedjson = [
{ name: "parent1", value: ["child11", "child12"] },
{ name: "parent2", value: ["child2"] },
{ name: "parent3", value: ["child3"] }
];
}
getit(){
const data = this.nestedjson;
let duplicatePushArray = [];
for(let i = 0; i < data.length ; i++){
if(duplicatePushArray.indexOf(data[i].name) === -1) {
duplicatePushArray.push(data[i].name);
} else {
console.log(`${data[i]} is already pushed into array`);
}
}
console.log('Final Array: ', duplicatePushArray)
/*output: [{"name":"parent1","value":["child11","child12"]},{"name":"parent2","value":["child2"]},{"name":"parent3","value":["child3"]}]*/
}
}
【问题讨论】:
-
是检查所有复选框后的输出吗?请分享一些场景和预期输出
-
“home.component.ts”注释代码中已经存在输出格式。选中复选框并单击仅提交后,我需要获取输出
-
输出 - [{"name":"parent1","value":["child11","child12"]},{"name":"parent2","value":[" child2"]},{"name":"parent3","value":["child3"]}]
-
@UIAPIDEVELOPER,如果只选择了 child11,您是否期望两个值 - 来自父级 1 - {"name":"parent1","value":["child11","child12"] 或只有 child11 - {"name":"parent1","value":["child11"]
-
{"name":"parent1","value":["child11","child12"]}
标签: html angular typescript