【问题标题】:Angular - Custom Input: reset touched state via form.reset() not workingAngular - 自定义输入:通过 form.reset() 重置触摸状态不起作用
【发布时间】:2017-07-04 13:36:56
【问题描述】:

我有一个关于重置带有自定义输入的表单的问题。

我将我的验证消息绑定到我的输入的 touched 属性。 虽然在默认输入上 form.reset() 还会将触摸状态重置为 false,但在自定义输入上,它不会被转发。

我添加了一个 plunkr https://plnkr.co/edit/ZyELDDFf3rxQRipHlWnb?p=preview 来说明问题:触摸并离开两个控件,都被触摸:true,然后重置。 如何将此清除重置事件(将触摸状态恢复为 false)转发到我的自定义输入?

@Component({
selector: 'my-app',
template: `
    <p><span class="boldspan">Form data:</span>{{demoForm.value | json}}</p>
    <p><span class="boldspan">Model data:</span> {{dataModel}}</p>
    <form #demoForm="ngForm">
      <custom-input name="someValue" [(ngModel)]="dataModel">
        custom input
      </custom-input>
      <label>native input<br>
      <input type="text" [(ngModel)]="someOtherValue" name="someOtherValue"
      #sOV="ngModel"></label>
      <div>touched: {{sOV.touched}}</div>
    </form>
    <br>
    <button (click)="demoForm.reset()">Reset Form</button>`
})
export class App {
    dataModel: string = '';
}

感谢和问候

【问题讨论】:

    标签: angular


    【解决方案1】:

    使用

    demoForm.resetForm()
    

    而不是

    demoForm.reset()
    

    【讨论】:

    • 感谢您的意见,但不幸的是,这并不能解决我遇到的问题。
    猜你喜欢
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-30
    • 2019-08-29
    • 1970-01-01
    相关资源
    最近更新 更多