Angular service is a function in which you use the same code across multiple components. You can share data across components, or you may fetch data from database of some sort.
This example generates a service file form angular-cli: ng g service name of service
.
We create a simple array and a mehod called myData() that returns a string. I show how you can access the properties and methods from another component. We do so with dependency injection and ngOnInit life cycle hook.
To view the array of data open dev tools and you will see the output from the console.log.
- Clone repository. Then locate top folder directory in command line
- Run
npm install
to install packages. - Run
ng serve
to start the server on port 4200.
- Angular 7,
- TypeScript,
- HTML,
- Angular-CLI, Angular Material(mat-tool, mat-list)