如何在 Angular 13 中使用 ngModel 进行双向绑定

问题描述 投票:0回答:1

我只是想从输入元素中获取用户输入并将其保存在变量中。

这就是我的组件打字稿的样子:

export class MyComponent {
 userInput: string = '';
}

这就是我的 html 的样子:

<input name="userInput" [(ngModel)]="userInput">

这就是我的 app.module.ts 的样子:

@NgModule({
  declarations: [
    AppComponent,
    MyComponent
  ],
  imports: [
    FormsModule
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

我收到这个错误:

ERROR TypeError: Cannot read properties of null (reading '_rawValidators')

我到处搜索但找不到任何关于这个特定错误的信息。有人有什么想法吗?

html angular typescript angular-forms ngmodel
1个回答
0
投票

我发现我的特殊问题涉及我的输入元素意外地位于不同的表单元素中。将输入元素移到表单元素之外解决了这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.