[Angular] Understanding the Angular Component providers property

Let's say we have App.component.ts, it use provider inside component level:

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
  providers: [LoadingService]
})
export class AppComponent implements  OnInit { }

 

What is mean is that all the child components under App component tree, will have 'LoadService' available. But the child component must appear in Template.

<app>
  <course-dialog></course-dialog>
</app>

In this case, if we want to use "LoadService" inside 'CouseDialog' component, we don't need to add 'providers' in 'CourseDialog' component.

 

But we don't have '<course-dialog>' inside template. Instead we open dialog component like:

const dialogRef = this.dialog.open(CourseDialogComponent, dialogConfig);

It open the dialog dynamicly. Then we have to use providers.

@Component({
  selector: 'courses-card-list',
  templateUrl: './courses-card-list.component.html',
  styleUrls: ['./courses-card-list.component.css'],
  providers: [LoaderService]
})
export class CoursesCardListComponent { }

 

posted @   Zhentiw  阅读(152)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2019-03-14 [Bash] Create Aliases in .bash_profile for Common Bash Commands
2019-03-14 [Algorithm] Radix Sort Algorithm
2018-03-14 [HTML 5] Styling with ARIA
2017-03-14 [Node.js] Initialize a LoopBack Node.js Project through the CLI
2017-03-14 [Angular] Pipes as providers
2017-03-14 [Angular] Create a custom pipe
2017-03-14 [Angular] Create a simple *ngFor
点击右上角即可分享
微信分享提示