Angular 2正确声明NgIf的方法(Angular 2 Proper way to declare NgIf)

编程入门 行业动态 更新时间:2024-10-25 02:21:31
Angular 2正确声明NgIf的方法(Angular 2 Proper way to declare NgIf)

我正在使用* ngIf在模板中有条件地显示<div>标签。

<div *ngIf="panel?.imageUrl"><img [src]="panel?.imageUrl" /></div>

因为我使用TypeScript,如果我没有在@Component指令中声明NgIf,则会出现“编译”时间错误“未使用的NgIf”。 如果我将NgIf添加到ETHER viewProviders或者providers或directives ( directives: [NgIf] ),它会编译并工作。 我的问题是:宣布NgIf的正确方法是什么?为什么?

I am using *ngIf to conditionally display a <div> tag in a template.

<div *ngIf="panel?.imageUrl"><img [src]="panel?.imageUrl" /></div>

Because I am using TypeScript, there is a "compile" time error "unused NgIf" if I do not declare NgIf in a @Component directive. It compiles and works if I add NgIf to ETHER viewProviders or providers or directives (directives: [NgIf]). My question is: what is the right way to declare NgIf and why?

最满意答案

无需向提供者添加NgIf 。 它们包含在PLATFORM_DIRECTIVES ,默认情况下可以在任何地方使用。

There is no need to add NgIf to providers. They are included in PLATFORM_DIRECTIVES which are available everywhere by default.

更多推荐

NgIf,ngIf,使用,电脑培训,计算机培训,IT培训"/> <meta name="description"

本文发布于:2023-07-28 18:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1307983.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正确   声明   方法   Angular   declare

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!