Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

angular call component method from service

// In the service
export class simpleService{

  private subject = new Subject<any>();
  
  constructor() { }
  // Service message commands
  
  // Read Service Data
  onReadData(data: any){
    this.subject.next(data);
  }
  
  // Send Service Data 
  onSendData() {
    return this.subject.asObservable();
  }
  
}


// In the component you want to send the data to

dataSubscription: Subscription;

constructor(private simpleService: SimpleService){
	this.dataSubscription = this.simpleService.onSendData().subscribe( async (data) => {
      // do whaterver you want with the incoming data
      console.log('incoming data: ', data);
    });
}
 
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #angular #call #component #method #service
ADD COMMENT
Topic
Name
7+1 =