简体   繁体   English

通过单击Angular 5中的按钮来清除/重置特定的输入字段

[英]Clear/Reset specific inputs fields by button click in Angular 5

<form #addOpeninghoursForm="ngForm" ngNativeValidate (ngSubmit)="addOpeninghours()">

<div class="form-group">
  <label for="day">Select Day:</label>
  <select class="form-control" [(ngModel)]="openinghours.day" name="day" id="day" required>
    <option value="1">Saturday</option>
    <option value="2">Sunday</option>
  </select>
</div>

<div class="form-group">
  <label for="opens_from">Opens From:</label>
  <input type="time" class="form-control" [(ngModel)]="openinghours.opens_from" name="opens_from" id="opens_from" placeholder="Enter the time" required>
</div>

<div class="form-group">
  <label for="opens_till">Opens Till:</label>
  <input type="time" class="form-control" [(ngModel)]="openinghours.opens_till" name="opens_till" id="opens_till" placeholder="Enter the time" required>
</div>

<button type="submit" class="btn btn-success">Save</button>
<button type="submit" class="btn btn-success" (click)="onlyResetTheTimeInputs?!?!">Reset</button>
</form>

When I saved the form, by pressing the save button, sometimes I want to press the reset button. 当我保存表单时,通过按保存按钮,有时我想按重设按钮。 Not always! 不总是! But when I have to, I only wanna clear the time inputs. 但是当我不得不的时候,我只想清除时间输入。 The earlier selected day may not be cleared. 较早选择的日期可能不会清除。

You can clear the input fields of time in onlyResetTheTimeInputs() like 您可以在onlyResetTheTimeInputs()中清除时间输入字段,例如

onlyResetTheTimeInputs(): void {
  this.openinghours.opens_from  =  this.openinghours.opens_till = '';
}

Take a look at the code and demo 看看代码和演示

Source, Stack Blitz 来源,Stack Blitz

Explanation: 说明:

  1. Just a simple clearing of value will do, angular takes care of the view. 只需简单地清除价值,角度就可以照顾到视图。
  2. Advantage of having two way data binding is view doesn't need to be updated manually 双向数据绑定的优点是视图不需要手动更新

     onlyResetTheTimeInputs() { this.openinghours.opens_from = this.openinghours.opens_till = null; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM