Reputation: 839
I'm new with Angular. I follow a tutorial from this link, and i tried to do/add something that doesn't part of that tutorial. I added a providers :[EmployeeService]
line to employee-list.component.ts
and empployee.component.ts
inside @component
, and it returns error that said TypeError: Cannot read property 'push' of undefined
. So realize that adding providers :[EmployeeService]
to some components are unnecessary. I read about Dependency Injection from this link but i do not really get what i wanted to know. So can anyone give me a simple explanation how/when/where to use/put providers and how that error happens?
Thank you very much in advance.
Upvotes: 0
Views: 109
Reputation: 5764
providers
array tells Angular what services it should instantiate. You can define it on two places:
When you use providers in component the service will be available to that and all it's children.
When you use it in module the service will be available for all pipes, directives, components and services in given module.
But you can define it in both if you want - in that case Angular will create two instances of the same service.
For beginning provide your services on the module level.
Upvotes: 1