Angular 5 检查变量的值和 \*ngIf 语句

新手上路,请多包涵

我正在使用 Angular 5,并且正在尝试检查组件的 html 模板中的变量值。

所以它看起来像这样:

 <div *ngIf="item='somevalue'">

我收到此错误:

ht 错误:模板解析错误:

 Parser Error: Bindings cannot contain assignments at column 17 in...

这不能用角度来完成吗?

原文由 user8770372 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 546
2 个回答

*ngIf 像这样工作 *ngIf="expression" 其中 expression 被简单的 Javascript 语句替换,该语句返回 boolean 。 But you use one = and it means that you assign the someValue to the item property and if the value is not falsy it will return you true .

在您的情况下,您需要编写 *ngIf="item === 'somevalue'"

原文由 Suren Srapyan 发布,翻译遵循 CC BY-SA 3.0 许可协议

您正在使用单个 = 运算符进行分配。

使用双等于 == 运算符来检查相等或更好地使用 === 来检查严格相等。

 <div *ngIf="item === 'somevalue'">

原文由 Gourav Pokharkar 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进