Understanding of services, HTTP, Observables and RxJs.
Angular Learning #1
Principles:
- Don’t Repeat yourself.
- Single Responsibility principle.
Service: Services are the solution for maintaining both principles.
- Service is a class with a specific purpose.
- Share data across multiple component
In this case services will be responsible for sharing data across multiple components.
3. Implement application logic.
Suppose an employee enters the date of birth and we need to calculate their age, we will write a logic to do this, which should be reusable code which should be independent of any individual component.
Naming Convention- .service.ts
For employee service- employee.service.ts and class name- EmployeeService
Dependencies Injection…
One component can be dependent on many services. We register all the dependencies/services with the injector. When components are initialised, the injector provides all the necessary dependencies for the proper functioning of the component.
Injectable decorator is used when you want to inject service into another service.
Dependency is specified in constructor.
Ex-
Angular Learning #2
HTTP, Observables and RxJs
- HTTP Get request from EmployeeService
- Receive the observable and convert it into an employee array.
- Subscribe to the observable from EmployeeList, EmployeeDetails …components
- Assign the employee array to a local variable
RxJs:
It is an external library that enables us to work with observables in angular application.
HTTP Mechanism in Angular:
Observables Explanation
Please put your questions in comment section.