我如何知道何时需要向app.module.ts添加内容?

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

我的app.module.ts需要添加FormsModule来运行我的模板代码我怎么知道它?

错误有点神秘,

Can't bind to 'ngModel' since it isn't a known property of 'input'. ("

有什么好处

import { NgModel } from '@angular/forms';

在我的组件中,如果我没有

import { FormsModule } from '@angular/forms';

在我的app.module.ts,并添加到imports阵列。显然,我现在知道它是必需的,但我怎么知道呢?

  • 这很容易记录在哪里?
  • 我应该注意哪些错误?

imports的作用相比,NgModel阵列的作用究竟是什么?

angular dependencies
2个回答
2
投票

每个模块都可以导出一组可声明的类型 - 组件,指令或管道。如果要使用模块中声明的任何声明类型而不是使用声明的模块,则需要导入该模块。

FormsModule导出ngModel指令,这就是你需要导入它的原因。

我建议你阅读Avoiding common confusions with modules in Angular

这很容易记录在哪里?

您可以去documentation of the directive并查看声明它的模块:

Module | import { NgModel } from '@angular/forms';

我应该注意哪些错误?

所有错误都具有相同的格式:

Can't bind to <attribute> since it isn't a known property of <element>

0
投票

如果您不想从浏览Angular教程或书籍开始,请阅读错误文本。在您的情况下,它声明ngModel不是输入的已知属性。显然,您有一个HTML输入字段,它显然没有ngModel属性。

然后谷歌在ngModel Angular和搜索结果中的第二个链接是这一个:https://angular.io/api/forms/NgModel。它描述了ngModel是什么,并声明您需要导入FormsModel。

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