- Expert Angular
- Mathieu Nayrolles Rajesh Gunasundaram Sridhar Rao
- 423字
- 2021-07-15 17:05:29
Dependency Injection
When an instance of a class is created, supplying the required dependencies of that class for it to function properly is called Dependency Injection. Angular provides a modern and improved version of Dependency Injection:

In Angular, the injector maintains the containers to hold the instances of the dependencies and serves them as and when required. If the instance of a dependency is not available in the container, then the injector creates an instance of the dependency and serves it:

As stated earlier, components have logic that is related to templates and mostly consume services to perform business logic. So, components depend on services. When we write code for components, we create a parameter constructor that takes the service as an argument. It means that creating an instance of the component depends on the service parameter in the constructor. Angular requests that the injector provide the instance of the service in the parameter of the constructor of the component. The injector will serve the instance of the requested service, if available; otherwise, it creates a new one and serves it:
export class BookComponent { constructor(private service: BookService) { } }
In this code snippet, the : symbol comes from TypeScript and is not Angular syntactical sugar. The private keyword is also from TypeScript and enables assigning the passed constructor to the class instance automatically. The type information is used to infer the type to be injected. The BookComponent has a dependency to BookService and is injected in the constructor. So when an instance of the BookComponent is created, Angular will also make sure the instance of BookService is readily available for the BookComponent instance to consume.
The injector has knowledge of the dependencies to be created from providers that are configured with the required dependency types when bootstrapping the application or when decorating the components, as follows:
@NgModule({ imports: [BrowserModule], declarations: [AppComponent,], providers: [BookService], bootstrap: [ AppComponent ] }) export class AppModule { }
The preceding code snippet adds BookService as a provider to the bootstrap function. The injector will create an instance of BookService and keep it available in the container for the entire application to inject whenever it's requested:
@Component({ providers: [BookService] }) export class BookComponent { ... }
The preceding code snippet adds BookService as a provider in the metadata of the component. The injector will create an instance of BookService when it encounters a request to create an instance of BookComponent.
We will discuss Dependency Injection and hierarchical Dependency Injection in detail in Chapter 12, Implementing Angular Services.
- JavaScript百煉成仙
- C語(yǔ)言程序設(shè)計(jì)案例教程(第2版)
- Java EE框架整合開發(fā)入門到實(shí)戰(zhàn):Spring+Spring MVC+MyBatis(微課版)
- 編程珠璣(續(xù))
- 軟件項(xiàng)目管理實(shí)用教程
- Big Data Analytics
- Express Web Application Development
- CoffeeScript Application Development Cookbook
- Kotlin開發(fā)教程(全2冊(cè))
- Python爬蟲、數(shù)據(jù)分析與可視化:工具詳解與案例實(shí)戰(zhàn)
- 運(yùn)維前線:一線運(yùn)維專家的運(yùn)維方法、技巧與實(shí)踐
- Cocos2d-x Game Development Blueprints
- JavaScript動(dòng)態(tài)網(wǎng)頁(yè)編程
- iOS開發(fā)項(xiàng)目化入門教程
- Vue.js 3.x高效前端開發(fā)(視頻教學(xué)版)