【问题标题】:Angular: strictTemplates - How to set boolean and numbers in Templates properly?Angular:strictTemplates - 如何在模板中正确设置布尔值和数字?
【发布时间】:2021-02-03 01:30:28
【问题描述】:

我设置 strictTemplates:true 并在 HTML 模板中使用以下内容

<textarea matInput matAutosizeMaxRows="4" matTextareaAutosize="true"

我从编译器收到

 error TS2322: Type 'string' is not assignable to type 'number'.
 error TS2322: Type 'string' is not assignable to type 'boolean'.

但是如何在 HTML-Templates 中正确设置它(以避免错误)?

【问题讨论】:

标签: angular angular-ivy


【解决方案1】:

将属性名称括在括号中。如果您离开方括号,这些值将被解释为字符串。使用括号,这些值被解释为 TypeScript,因此可以正确输入。

<textarea matInput [matAutosizeMaxRows]="4" [matTextareaAutosize]="true"

【讨论】:

  • 感谢您的回复。是否有一般指南如何“新”处理 HTML 变量?我在 $events 或 enums 上遇到了类似的问题
  • 所有期望字符串的值都应该使用括号表示法。对于事件,您需要注意正确输入事件处理函数的参数。但是如果没有代码,很难说出问题所在。也许添加一个新问题?
猜你喜欢
  • 2022-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-13
  • 2017-06-30
  • 2017-06-01
相关资源
最近更新 更多