以角度2重置模板驱动表单(模型和验证两者)

时间:2016-10-01 11:17:10

标签: angular angular2-forms

我们如何使用角度2中的模板驱动形式的验证状态重置控件? 我知道可以通过设置绑定到的模型值来重置控件。但是验证状态(原始,脏等等)呢?

我试过这样的事情:

onSubmit(playlistForm: any) {
// ...
  playlistForm.form.reset();
}

在控制器中,

''

但在上面似乎实际上是重定向到EXCEPTION: Error: Uncaught (in promise): Error: Cannot match any routes: '' 我收到以下错误:

var
  PW : String;
  Error : Integer;

const
  PWIsOk = 0;
  PWIsBlank = 1;
  PWTooLong = 2;
  PWContainsDigit = 3;

procedure CheckPassword;
var
  i : Integer;
begin

  writeln;
  writeln('Ok, please enter your future password.');
  writeln('Attention: The Text can only be decoded with the same PW');
  writeln('Your password must be between 1 and 25 characters long and contain no digits.');

  repeat
    error := PWIsOk;
    readln(PW);

    if Length(PW) = 0 then
      Error := PWIsBlank;

    if Error = PWIsOk then begin
      if Length(PW) > 25 then
        Error := PWTooLong;
      if Error = 0 then begin
        for i := 1 to Length(PW) do begin
          if (PW[i] in ['0'..'9']) then begin
            Error := PWContainsDigit;
            Break;
          end;
        end;
      end;
    end;

    case Error of
      PWIsOK : writeln('Password is ok.');
      PWIsBlank : writeln('Password cannot be blank.');
      PWTooLong : writeln('Password is too long.');
      PWContainsDigit : writeln('Password should not contain a digit');
    end; { case}
  until Error = PWIsOk;
  writeln('Done');
end;

那我怎么做我想要的呢?

6 个答案:

答案 0 :(得分:9)

这两个对我有用:

playlistForm.reset();
playlistForm.resetForm(); // I think this is the one to use

Angular docs只有resetForm() https://angular.io/docs/ts/latest/api/forms/index/NgForm-directive.html

答案 1 :(得分:3)

<form (ngSubmit)="onSubmit(playlistForm)" #playlistForm="ngForm">
// Some code here...
</form>


onSubmit(playlistForm: ngForm) {
// ...
  playlistForm.form.reset();
}

答案 2 :(得分:2)

这对我有用:

<form (ngSubmit)="onSubmit(playlistForm);playlistForm.reset()" #playlistForm="ngForm">
    // Some code here...
</form>

答案 3 :(得分:0)

为我工作的onClear()是执行一些其他任务所需的附加回调。

$ npm install -g wolkenkit

答案 4 :(得分:0)

form.resetForm(); 

form.reset()不同,这还会重置表单的验证状态。适用于模板驱动和反应性方法。

答案 5 :(得分:0)

我在 .ts 中使用了原生元素引用,稍后调用 form 清除了使用 reset 方法提交后的表单数据