输入字段未重置值 - AngularJS 2

时间:2016-02-14 12:51:57

标签: typescript angular

export class ParametersForm {
  myForm: ControlGroup;
  systemParameters: AbstractControl;
  arr: number[];
  constructor(fb: FormBuilder) {
    this.myForm = fb.group({
      "realisations" : [""],
      "numConstSteps" : [""],
      "timeHorizon": [""],
      "continuationStep" : [""],
      "continuationStepSign" : [""],
      "numberOfModelParameters" : [""],
      "systemParameters" : [],
      "param" : [""],
      "netLogoFile" : [""],
      "numberOfModelVariables" : [""],
      "restrictOperator" : [""],
      "liftOperator" : [""],
      "xInitial" : [""]

    });
    this.arr = [];
    this.systemParameters = this.myForm.controls["systemParameters"];
  }
  addToArray(value: any): void {
    this.arr.push(value);
    this.systemParameters = null;
  }
  onSubmit(form: any): void {
    console.log(this.arr);
    form.systemParameters = this.arr;
    console.log("your submitted value:", form)
  }
}

形式

@Component({
    selector: 'parameters-form',
    directives: [FORM_DIRECTIVES],
    template: `
      <h1>Parameters Form</h1>

      <form [ngFormModel]="myForm" (ngSubmit)="onSubmit(myForm.value)" class="ui form">

<div class="field">
            <label for="systemParameters">System Parameterss</label>
            <input type="number"
                   id="systemParameters"
                   placeholder="systemParameters Param"
                   [ngFormControl]="myForm.controls['systemParameters']" #systemParam>

            <button type="button" (click)="addToArray(systemParam.value)">Add</button>
          </div>

<button type="submit" class="ui button">Submit</button>
      </form>
    `
})

问题是,当我点击“添加”按钮时,该字段没有重置,这样我就可以输入我的下一个值而不删除旧值。

1 个答案:

答案 0 :(得分:1)

而不是

this.systemParameters = null;

这应该有效

import { Control } from "angular2/common";

(<Control>this.systemParameters).updateValue('');