angular - "[(ngModel)]"在 Ionic 4 中未绑定(bind)

标签 angular ionic-framework ionic4

我正在构建一个 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/

相关文章:

ionic-framework - 使用新的 Ionic 导航(第 4 版)

html - 弹出窗口 : Toolbar within ion-header doesn’t stay fixed

angular - 如何在延迟加载的 primeng turbotable 中使用过滤器?

angular - 从 ts 页面重置 p 表

angular - ag-grid cellRenderer 在值评估后渲染单元格失败

node.js - 由于依赖问题, ionic 启动失败

angular - 无法绑定(bind)到 'items',因为它不是 'virtual-scroller' 的已知属性

javascript - 将第三方 JavaScript 文件添加到 Angular 4

ios - 当应用程序通过应用程序商店更新时,在cordova应用程序中保留本地存储

javascript - Ionic 4 上传截图到服务器