+1 vote
in Angular by

What is Dependency Injection? Explain with example.

1 Answer

0 votes
by
Dependency injection is the ability to add the functionality of components at runtime. Let's take a look at an example and the steps used to implement dependency injection.

Step 1 − Create a separate class which has the injectable decorator. The injectable decorator allows the functionality of this class to be injected and used in any Angular JS module.

@Injectable()

   export class classname {  

}

Step 2 − Next in your appComponent module or the module in which you want to use the service, you need to define it as a provider in the @Component decorator.

@Component ({  

   providers : [classname]

})

Related questions

0 votes
asked Aug 11, 2023 in Angular by DavidAnderson
0 votes
asked Nov 4, 2023 in ReactJS by AdilsonLima
...