pablocattaneo
6/27/2017 - 11:43 AM

How to use a Service, See the comment to figure out.

How to use a Service, See the comment to figure out.

// We have to define the constructor that angular is going to use when instanciate the class
// We have to define constructor, providers, import the class and use it

import { LoggingService } from './../logging.service'; // Import the service
import { Component, EventEmitter, Output } from '@angular/core';

@Component({
  selector: 'app-new-account',
  templateUrl: './new-account.component.html',
  styleUrls: ['./new-account.component.css'],
  providers: [LoggingService] // We have to inform angular about our dependency
})
export class NewAccountComponent {
  @Output() accountAdded = new EventEmitter<{name: string, status: string}>();

  // The type of the parameter must be the class of the service
  constructor(private loggingService: LoggingService) {}

  onCreateAccount(accountName: string, accountStatus: string) {
    this.accountAdded.emit({
      name: accountName,
      status: accountStatus
    });
    this.loggingService.logStatusChange(accountStatus); // Use the method of the service
  }
}