Data class services

For the sake of reusability and maintainability, we need to abstract the logic concerns out of the component and let the component just serve as a presentation layer. This is one of those use cases where TypeScript services in Angular come into play.

You first need to create a service with the following command:

ng g service comment

This creates your service class, ./src/app/comment.service.ts, with a scaffold content. Update the content with the following:

import { Injectable } from '@angular/core';@Injectable()export class CommentService {  private comments: Array<any> = [    {      author: 'solomon',      content: `TypeScript + Angular is amazing`    },    {      author: 'lorna',      content: `TypeScript is really awesome`    },    { author: 'codebeast', ...

Get TypeScript 2.x for Angular Developers now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.