Introduction to services and dependency injection

Introduction to services and dependency injection

service is a code that are used to perform a specific task, a service can contain a value or function or combination of both.

Service is injected into the application using dependency injection mechanism.

Service provides its data and methods other needs components.
How to create a service:- you can easily generate service. Open your terminal and type:

ng generate service auth

Import the Injectable Member:-At the top of your new service file, add:

import { Injectable }from'@angular/core';

Include it through dependency injection:- In the constructor argument of the component class, we include it through dependency injection:

private afAuth: AngularFireAuth,
private router: Router,
private afs: AngularFirestore
) {}

Create a Method and use another component:-So, guys, you can finally create the method and use need components.

So let’s go and see the little example.


emailSignUp(email: string, password: string) {
return this.afAuth.auth.createUserWithEmailAndPassword(email, password);


onSubmit(model, f) {
this.auth.emailSignUp(, model.password).then(res => {'sign up success', '', { duration: 3000 });
}).catch(err => {, '', { duration: 3000 });

                      -:Thanks for the reading:-
Next Post »


Click here for comments
M. Taha
May 28, 2020 at 2:32 AM ×

I learn some new stuff from it too, thanks for sharing your information. 2k moulding

Congrats bro M. Taha you got PERTAMAX...! hehehehe...