简体   繁体   中英

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....?

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM