如何重设控制值? [英] How to reset Control value?
本文介绍了如何重设控制值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图控制我们在哪里可以输入带有标签及其得分的多个玩家.
I'm trying to make a control where we can enter multiple player with tag and their score.
但是,当我们单击AddPlayer按钮时,我创建了一个播放器,然后需要将表单控件值重置为默认值.
But when we click on my AddPlayer Button I create a player then I need to reset the form control value to default.
我尝试了很多事情,但是没有任何效果..它永远不会改变视图值.
I've tried a lot of things but nothing works .... it never changes the view value.
这是代码:
addPlayer(form: ControlGroup) {
var player = new Player();
player.tag = form.value.tag;
player.name = form.value.name;
player.score = form.value.score;
// nothing work
form.value = null;
form.value.tag = null;
form.value.tag = '';
this.playerService.addPlayer(player.tag, player.name, player.score);
this.newplayer.next(player);
}
这是html
<form (submit)="addPlayer(playerForm)" [ng-form-model]="playerForm">
<div class="form-group" [class.has-error]="!playerForm.find('tag').valid && playerForm.find('tag').touched">
<div class="col-md-3 text-right">
<label for="tag">Tag: </label>
</div>
<input type="text" id="tag" #tag="form" [ng-form-control]="playerForm.controls['tag']" placeholder="Tag"/>
<span *ng-if="tag.control.hasError('required') && !tag.control.pristine">Tag is required</span>
</div>
<div class="form-group" [class.has-error]="!playerForm.find('name').valid && playerForm.find('name').touched">
<div class="col-md-3 text-right">
<label for="name">Player Name: </label>
</div>
<input type="text" id="name" #name="form" [ng-form-control]="playerForm.controls['name']" placeholder="Player Name" />
<span *ng-if="name.control.hasError('required') && !name.control.pristine">Player Name is required</span>
</div>
<div class="form-group" [class.has-error]="!playerForm.find('score').valid && playerForm.find('score').touched">
<div class="col-md-3 text-right">
<label for="score">Score: </label>
</div>
<input type="number" id="score" #score="form" [ng-form-control]="playerForm.controls['score']" value="0" min="0" max="200" />
<span *ng-if="score.control.hasError('required') && !score.control.pristine">Score is required</span>
</div>
<button type="submit" class="btn btn-primary">
Add Player
</button>
</form>
那么我该如何重置控件的值?
So how can i reset the value of the control?
推荐答案
要重置表单,最简单的方法是重建表单生成器. 单击添加播放器按钮后
To reset the form the easiest way is rebuild the form builder. After clicking the add player button
this.playerForm = this.builder.group({
'name': [...],
'tag': [...],
'score': [...]
});
这篇关于如何重设控制值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文