TikChiku
TikChiku

Reputation: 403

Angular2: sending value from component to service

I would like to send the value from component to service

〇Component

import { ColumnEquipedService } from '../../../service/column-equiped/column-equiped.service';

export class SampleComponent{
  public componentCd = `wa9001`;
   、、、、、
}

〇Service

    import { Injectable } from '@angular/core';
    @Injectable()
    export class ColumnEquipedService {
          componentCd: string;
    、、、、
}

want to send the value of componentCd in sample component to componentCd in ColumnEquipedService.

How its supposed to be done....?

Upvotes: 1

Views: 45

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222572

Have a method defined to save the id to the variable

export class ColumnEquipedService {
  componentCd: string;
 constructor() 
  saveCompoId(compId:string){
   this.componentCd = compId;
 }
}

And then in the component, call the method.

constructor(private myService : ColumnEquipedService)

saveToService(){
   myService.saveCompoId(this.componentCd);
}

Upvotes: 3

Related Questions