我正在构建一个 Ionic 应用程序,目前正在从 Ionic 3 迁移到 4。我有一些代码在 Ionic 3 中完美运行,但在 Ionic 4 中却没有。
基本上,我似乎无法在输入字段中将数据与 [(ngModel)] 绑定(bind)。我已导入 FormsModule
并将其添加到我的 app.module.ts
中的导入中.这是我的-component.ts
文件看起来像:
export class SomeComponent implements OnInit {
someString: string = "test";
...
}
还有我的
-component.html
<ion-input type="text" [(ngModel)]="someString"></ion-input>
{{someString}}
现在在我的应用程序中,
{{someString}}
正确显示“测试”。但是,更改输入值不会影响 someString
以任何方式变化。我会错过什么?
最佳答案
注意:这是一种解决方法,而不是解决方案。请参阅下面的编辑
奇怪的是,Ionic 4 input documentation没有提到ngModel
,而是谈论 value
属性。所以我想,我会替换 [(ngModel)]
与 [value]
.
<ion-input type="text" [value]="someString"></ion-input>
为了能够在我的 -component.ts
中访问该值文件,我做了类似的事情:-component.html
<ion-input type="text" [value]="someString" (input)="do_something($event)"></ion-input>
-component.ts do_something($event) {
this.someString = $event.target.value;
}
编辑 在使用上述解决方案之前,请确定您使用的是
[(ngModel)]
而不是 [ngModel]
.与我的问题所说的相反,[(ngModel)]
确实有效。我宁愿使用 [ngModel]
关于angular - "[(ngModel)]"在 Ionic 4 中未绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60013580/