【问题标题】:How to change an input text and store it afterwards? [duplicate]如何更改输入文本并在之后存储它? [复制]
【发布时间】:2019-12-20 09:36:12
【问题描述】:

我有一个输入字段,用户应该可以在其中更改他的姓名。那么可以操作该字段,并在终止时将新字符串存储为新用户名?我尝试使用form 实现这一目标,但我收到此错误:Error: Template parse errors: Can't bind to 'formGroup' since it isn't a known property of 'form'

这是我的代码

page.html

<form [formGroup]="form" (ngSubmit)="changeName()">
    <ion-list no-border>

        <ion-list-header>
          My Account
        </ion-list-header>

        <ion-item >
          ???? 
       <ion-input>{{username}}</ion-input>
        </ion-item>
  ...

      </ion-list>
    </form>

page.ts

import { FormGroup, FormControl, Validators} from '@angular/forms';
...
form: FormGroup;

public usernameInput = '';
...

constructor() {}

...

changeName() {
  this.usernameInput = '';
}

 ngOnInit() {
    this.form = new FormGroup({
     username: new FormControl(null, {
       updateOn: 'submit',
       validators: [Validators.required, Validators.maxLength(20), Validators.minLength(1)]
     })
 });
}

【问题讨论】:

  • 你导入了表单模块吗?

标签: javascript html angular typescript ionic-framework


【解决方案1】:

ReactiveFormsModule 导入您的功能模块。

【讨论】:

  • 很好,但是当我离开页面并再次返回时,表单没有存储。我总是有一个空的输入字段
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-08
  • 1970-01-01
  • 2016-09-20
  • 2015-11-22
  • 2020-06-02
相关资源
最近更新 更多