0

I am working on a simple music app. Now in the app I have two components , one is a home componentA that contains the audio player(meaning the volume bar , seek bar , play pause etc..) and the other componentB that has all the songs.

My objective is that whenever someone clicks on a song on componentB(that is a list of songs) immediately the details of that song(url,img etc..) are sent to componentA so that the song start playing.

this is my componentB.html

<div *ngFor="let song of songs | filter:searchText" 
      (click)="songSelected(song.url,song.name)">
      <img src="{{song.img}}">
      <div>
        <p><strong>{{song.name}}</strong><br>
        <h6>{{song.singer}}</h6>
      </div>
    </div>

songSelected() above is working fine.

This is my componentB.ts

  songSelected(url: string,name:string){
      this.selectedurl=url
      this.selectedname=name
      this._allsongs.setData(this.selectedurl,this.selectedname);
      
  }
  constructor(public _allsongs:AllsongsService) { }

  ngOnInit(): void {
    this.songs=this._allsongs.getSongs();
  }

and this is my service.ts

setData(url: any, name: any) {
    console.log("From Service set data previous = " + this.name)
    this.url = url;
    this.name = name;
    console.log("From Service set data new = " + this.name)
  }

  getData(): any {
    return [this.url, this.name]
    
  }

And this is my componentA.ts

ngOnInit(): void {
   this.songurl=this._allsongs.getData()[0];
   this.songname=this._allsongs.getData()[1];
  }

NOw what problem I am facing is that ngOnInit only works when component is initialised so when someone clicks on the song in componentB the song details are not sent using getdata. Please help me to figure this out.

Giannis
  • 1,790
  • 1
  • 11
  • 29
VATSAL JAIN
  • 561
  • 3
  • 18

1 Answers1

1

you need use in your service a Subject and subscribe in your component A, see an example of Subject in this SO

Your service

  private mySubjectSource = new Subject<any>();
  myObservable=this.mySubjectSource.asObservable();

  constructor(){}

  sendValue(value:any)
  {
    this.mySubjectSource.next(value);
  }

Your component A, in ngOnInit

  constructor(private myService: MyService) {}
  ngOnInit()
  {
    this.myService.myObservable.subscribe(res=>{
      ..make something with the response...
    })
  }

Your component B

   constructor(private myService: MyService) {}
   sendValue(value:any)
   {
     this.myService.sendValue(value);
   }

A fool example

Eliseo
  • 50,109
  • 4
  • 29
  • 67
  • Ok But how do I send the data I want to send instead of (value:any) I want to send multiple values like url name img as strings – VATSAL JAIN Jul 08 '21 at 08:16
  • 1
    "value" can be an object: `(click)="sendValue(song)">` – Eliseo Jul 08 '21 at 08:20
  • Can u give an example in my case pls – VATSAL JAIN Jul 08 '21 at 08:22
  • 1
    it's a fool example, but I hope can help you: https://stackblitz.com/edit/angular-cdvrjs?file=src%2Fapp%2Flist-song.component.ts – Eliseo Jul 08 '21 at 08:42
  • Instead of sending an object can I send multiple variables like instead of song I want to send songurl and songimage seperately so how odes it take two or more parameters – VATSAL JAIN Jul 08 '21 at 09:03
  • 1
    you can create "onfly" the object, but you only can send an object or a variable, but you can destructuring the response. I updated the stackblitz to show an example – Eliseo Jul 08 '21 at 09:38