Angular手动更新ngModel并将表单设置为脏或无效?

时间:2017-12-01 08:09:48

标签: angular typescript angular4-forms

我有一个表格和这样的基础模型

来自组件

<form #testForm="ngForm" id="testForm">
  <input type="text" id="myText" [(ngModel)]="myTextModel" name="myText" #myText="ngModel">
</form>
<button (click)="updateMyTextModel()">Update myTextModel</button>
<div *ngIf="testForm.dirty">testForm diry</div>
<div *ngIf="testForm.touched">testForm touched</div>

Html模板

  <connectionStrings>
    <add name ="cn" connectionString="User ID=YOUR_USER_HERE; Password=YOUR_PASS_HERE;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=.)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=.)));"/>
  </connectionStrings>

如何从代码中设置触摸或脏的表单?

注意:在此示例中,我使用按钮来触发模型更改,但我也可能以其他方式更新模型,例如在来自web api异步请求的回调中。

5 个答案:

答案 0 :(得分:7)

解决方案:

//our root app component
import {Component, NgModule, VERSION} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import { Component, ViewChild } from '@angular/core';
import { FormsModule }   from '@angular/forms';

@Component({
  selector: 'my-app',
  template: `
    <form #testForm="ngForm" id="testForm">
        <input type="text" id="myText" [(ngModel)]="myTextModel" name="myText" #myText="ngModel">
    </form>
    <button (click)="updateMyTextModel()">Update myTextModel</button>
    <div *ngIf="testForm.dirty">testForm diry</div>
    <div *ngIf="testForm.touched">testForm touched</div>
  `,
})
export class App {

  @ViewChild('testForm') test: any;

  updateMyTextModel(){
    this.test.control.markAsTouched();
    this.test.control.markAsDirty();

  }

  constructor() {
    console.log(this.test);
  }
}

@NgModule({
  imports: [ BrowserModule,FormsModule ],
  declarations: [ App ],
  bootstrap: [ App ]
})
export class AppModule {}

Plunkr工作:

https://plnkr.co/edit/YthHCEp6iTfGPVcNr0JF?p=preview

答案 1 :(得分:3)

为什么不使用Reactive表单(FormGroup),

let testForm = new FormGroup({
    myText: new FormControl('initial value')
})

<form [formGroup]="testForm">
    <input type="text" formControlName="myText">
</form>

<button (click)="updateMyTextModel()">Update myTextModel</button>
<div *ngIf="testForm.dirty">testForm diry</div>
<div *ngIf="testForm.touched">testForm touched</div>

在您的功能中,您可以根据您想要的任何条件使用markAsDirty()

updateMyTextModel(): void {
    this.myTextModel = "updated model value";
    if ( // some condition ) {
        this.testForm.markAsDirty();
    }
}

要将单个表单控件设置为脏/触摸,可以使用

this.testForm.get('myText').markAsDirty();
this.testForm.get('myText').markAsTouched();

答案 2 :(得分:1)

这应该有效:

@ViewChild('testForm') testForm;


updateMyTextModel(): void {
    this.myTextModel = "updated model value";
    this.myForm.form.markAsDirty();
}

答案 3 :(得分:0)

如果您使用NgForm形式的引用,例如-

@ViewChild('viewChildForm') public viewChildForm: NgForm;,然后尝试以编程方式在.ts文件中更改表单:

  • 要将表单设置为无效:this.viewChildForm.form.setErrors({ 'invalid': true });

  • 要设置为有效:this.viewChildForm.form.setErrors(null);

答案 4 :(得分:0)

如果您需要遍历表单中的所有输入字段并将其标记为触摸或脏:

onSubmit(nameForm)
{
    let inputAryVar = nameForm.form.controls
    for(let keyVar in inputAryVar)
    {
        inputAryVar[keyVar].markAsTouched();
        inputAryVar[keyVar].markAsDirty();
    }
}
相关问题