英文:
Angular Form Containing input values that should create a JSON object with other JSON objects as atributes
问题
我尝试创建一个表单,该表单将作为其值返回一个包含对象列表的JSON对象作为属性。表单值的JSON示例:
{
"name": "contestName",
"teams": [
{
"name": "team1",
"wins": 1,
"loses": 1
},
{
"name": "team2",
"wins": 2,
"loses": 2
}
]
}
我的表单如下:
<form id="form" #ContestForm="ngForm" (ngSubmit)="addNewContest(ContestForm.value)">
<div class="form-group" [(ngModel)]="contest">
<input type="text" [(ngModel)]="name" class="form-control" placeholder="Contest Name">
</div>
<div class="form-group" [(ngModel)]="teams" *ngFor="let i of [].constructor(2)">
<label>Team {{i}}:</label>
<input type="text" [(ngModel)]="name" class="form-control" placeholder="Name">
<div class="form-group">
<input type="number" [(ngModel)]="wins" class="form-control" placeholder="Wins">
</div>
<div class="form-group">
<input type="number" [(ngModel)]="loses" class="form-control" placeholder="Loses">
</div>
</div>
</form>
我尝试过使用typescript中的createObjectFromForm
函数设置值,该函数将单独分配值并返回对象,但没有结果。我希望有人面对过这个问题,因为我认为这是一个相当常见的问题。
英文:
Im trying to create a form that will return as it
s value an JSON object that contains as an attribute a list of objects. Form value JSON example :
{
"name" : "contestName",
"teams" :[
{
"name":"team1",
"wins":1,
"loses":1},
{
"name":"team2",
"wins":2,
"loses":2}
]
}
My Form Looks like :
<form id="form" #ContestForm="ngForm" (ngSubmit)="addNewContest(ContestForm.value)">
<div class="form-group" [(ngModel)]="contest" >
<input type="Contest Name" [(ngModel)]="name" class="form-control" placeholder="Contest Name">
<br>
<div class="form-group" [(ngModel)]="teams" *ngFor="let i of [].constructor(2)">
<br>
<label>Team {{i}} :</label>
<br><br>
<input type="Name" [(ngModel)]="name" class="form-control" placeholder="Name">
<br>
<div class="form-group">
<input type="Wins" [(ngModel)]="wins" class="form-control" placeholder="Wins">
</div>
<br>
<div class="form-group">
<input type="Loses" [(ngModel)]="loses" class="form-control" placeholder="Loses">
</div>
</div>
<br>
</div>
</form>
I tried this and setting the values through typescript in a createObjectFromForm function that would individually assign the values and return the object but got no results, I am hoping someone faced this issue as I think it`s a pretty common one.
答案1
得分: 0
感谢Brandon Taylor,我使用了响应式表单和表单组,达到了期望的结果!示例:
表单(在HTML文件中):
<form [formGroup]="contestForm" (ngSubmit)="alert()">
<input formControlName="name"/>
<div formArrayName="teams" *ngFor="let team of getTeamsControls(); index as i">
<div formGroupName="{{i}}">
<label>姓名</label>
<input type="text" id="name" name="name" formControlName="name">
<label>胜利</label>
<input type="text" id="wins" name="wins" formControlName="wins">
<label>失败</label>
<input type="text" id="loses" name="loses" formControlName="loses">
</div>
</div>
<p>
<button type="submit">提交</button>
</p>
</form>
表单声明和getTeamsControls()
函数(在TypeScript文件中):
contestForm = new FormGroup({
name: new FormControl(''),
teams: new FormArray([
new FormGroup({
name: new FormControl(''),
wins: new FormControl(''),
loses: new FormControl('')
}),
new FormGroup({
name: new FormControl(''),
wins: new FormControl(''),
loses: new FormControl('')
})
])
})
getTeamsControls() {
return (<FormArray>this.contestForm.get('teams')).controls;
}
请注意,以上是您提供的代码的翻译部分。
英文:
Thanks Brandon Taylor, I used a reactive form and form groups and reached the desired result! example :
form(in html file) :
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<form [formGroup]="contestForm" (ngSubmit)="alert()">
<input formControlName="name"/>
<div formArrayName="teams" *ngFor="let team of getTeamsControls(); index as i">
<div formGroupName="{{i}}">
<label> name </label>
<input type="text" id="name" name="name" formControlName="name">
<label> wins </label>
<input type="text" id="wins" name="wins" formControlName="wins">
<label> loses </label>
<input type="text" id="loses" name="loses" formControlName="loses">
</div>
</div>
<p>
<button type="submit">Submit</button>
</p>
</form>
<!-- end snippet -->
form declaration and getTeamsControls(); function (in typescript file):
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-typescript -->
contestForm = new FormGroup({
name: new FormControl(''),
teams: new FormArray([
new FormGroup({
name : new FormControl(''),
wins : new FormControl(''),
loses : new FormControl('')
})
,
new FormGroup({
name : new FormControl(''),
wins : new FormControl(''),
loses : new FormControl('')
})
])
})
getTeamsControls() { return (<FormArray>this.contestForm.get('teams')).controls;}
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论