Html 属性值声明一个或提供初始化器“谢谢你的回答,我在我的项目中实现了它的工作正常,但是如果我选中并取消选中它,然后再次选中其他单选框(对于parent1),然后单击submit按钮,它在选中的值控制台中显示为true。log(数据),它不像checkbox那样
Html 属性值声明一个或提供初始化器“谢谢你的回答,我在我的项目中实现了它的工作正常,但是如果我选中并取消选中它,然后再次选中其他单选框(对于parent1),然后单击submit按钮,它在选中的值控制台中显示为true。log(数据),它不像checkbox那样,html,angular,typescript,Html,Angular,Typescript,属性值声明一个或提供初始化器“谢谢你的回答,我在我的项目中实现了它的工作正常,但是如果我选中并取消选中它,然后再次选中其他单选框(对于parent1),然后单击submit按钮,它在选中的值控制台中显示为true。log(数据),它不像checkbox那样工作。我的意思是checked状态没有正确显示,下面是修改{{child}@UIAPPDEVELOPER,所以它对复选框工作正常,单选按钮失败?您能帮我一下吗?这里我主要关心的是在提交时捕获选中的值,而不是在更改或选择时,当我的复选框被预先选中
属性值声明一个或提供初始化器“谢谢你的回答,我在我的项目中实现了它的工作正常,但是如果我选中并取消选中它,然后再次选中其他单选框(对于parent1),然后单击submit按钮,它在选中的值控制台中显示为true。log(数据),它不像checkbox那样工作。我的意思是checked状态没有正确显示,下面是修改
<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>
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"]}]*/
}
}
<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; let i = index">{{child}}<span class="pull-right"><input type="checkbox" (change)="item.checked[i] = !item.checked[i]"></span></li>
</ul>
</li>
</ul>
<div><button type="submit" (click)="getit()">submit</button></div>
</div>
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
data:any;
nestedjson:any;
message = '';
test:any;
constructor(private formBuilder: FormBuilder) {
}
ngOnInit() {
this.nestedjson = [
{ name: "parent1", value: ["child11", "child12"] },
{ name: "parent2", value: ["child2"] },
{ name: "parent3", value: ["child3"] }
];
this.nestedjson.forEach(v => v.checked = Array(v.value.length).fill(false));
}
getit(){
const data = this.nestedjson;
let duplicatePushArray = this.nestedjson.reduce((acc, v) => {
let temp = {name: v.name, value: []};
v.checked.forEach((val, i) => {
if(val){
temp.value.push(v.value[i]);
}
})
if(temp.value.length > 0){
acc.push(temp)
}
return acc
}, []);
console.log('Final Array: ', duplicatePushArray)
/*output: [{"name":"parent1","value":["child11","child12"]},{"name":"parent2","value":["child2"]},{"name":"parent3","value":["child3"]}]*/
}
}