How to use hierarchical dependency injection to provide different service instances

0 votes
Can i know How to use hierarchical dependency injection to provide different service instances?
6 days ago in Node-js by Nidhi
• 15,620 points
57 views

1 answer to this question.

0 votes

To use hierarchical dependency injection in Angular and provide different instances of a service:

How It Works:

Angular creates new instances of a service if you provide it at a lower level (like component), overriding higher-level (e.g., root) instances.

Example:

Default service (singleton):

@Injectable({ providedIn: 'root' })

export class LoggerService {

  log(msg: string) {

    console.log(msg);

  }

}

Override in a specific component:

@Component({

  selector: 'app-child',

  templateUrl: './child.component.html',

  providers: [LoggerService] // New instance here

})

export class ChildComponent {

  constructor(private logger: LoggerService) {

    this.logger.log('Child logger instance');

  }

}

answered 3 days ago by anonymous

Related Questions In Node-js

0 votes
1 answer

How to use executables from a package installed locally in node_modules?

Hello @kartik, Use the npm bin command to get the ...READ MORE

answered Jul 13, 2020 in Node-js by Niroj
• 82,840 points
1,731 views
0 votes
1 answer

How to use MongoDB with promises in Node.js?

Hello @kartik, Try this: var MongoClient = require('mongodb').MongoClient var url ...READ MORE

answered Oct 12, 2020 in Node-js by Niroj
• 82,840 points
1,635 views
0 votes
1 answer

How to use an include with attributes with sequelize?

Hello @kartik, Something like this should work foo.findAll({ ...READ MORE

answered Oct 13, 2020 in Node-js by Niroj
• 82,840 points
15,488 views
0 votes
1 answer

How to provide a mysql database connection in single file in nodejs?

Hello @kartik, You could create a db wrapper ...READ MORE

answered Oct 15, 2020 in Node-js by Niroj
• 82,840 points
9,475 views
0 votes
1 answer
0 votes
1 answer

How to design a pipe that accepts configuration options for flexible transformations?

Angular Pipe Implementation import { Pipe, PipeTransform } ...READ MORE

answered 3 days ago in Node-js by anonymous
31 views
0 votes
1 answer

How to create a service that manages user sessions and authentication tokens?

1. Create the Auth Service (auth.service.ts) import { ...READ MORE

answered 3 days ago in Node-js by anonymous
36 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to use ngIf with else?

You can use *ngIf with else to ...READ MORE

answered Feb 26 in Node-js by Navya
89 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP