Local service for components in angular 5

3343 views angular5

I want to put all my logic in service from component. Is there way to have local service for each component

answered question

1 Answer


If you want local instance for your component , what you can do is - you can use Providers:[SimpleService] metadata in you @component decorator.

 selector: 'parent',
 template: `...`,
 providers: [ SimpleService ]
class ParentComponent {
  constructor(private service: SimpleService) { }

Each instance of ParentComponent now has it’s own instance of SimpleService, so state is not shared globally but only between a ParentComponent and it’s child components.

posted this

Have an answer?


Please login first before posting an answer.