How to passing parameter from component.ts to server.ts using ionic angular?

Issue

I want to get the id from route and use it in my detail_kejadian function to get data from database.
it shows error Undefined index id, and Http failure during parsing for..
What’s wrong with it?

This is my component.ts file

export class DetailkejadianComponent implements OnInit {

  constructor(public ks:KejadianService, public route:ActivatedRoute) { }

  ngOnInit() { 

    var idkejadian:number=this.route.snapshot.params['id'];
    this.detail_Kejadian(idkejadian);
  }

  detailkej = [];

  detail_Kejadian(id){
    this.ks.detailKejadian(id).subscribe(
      (data) => {this.detailkej = data}
    );
  }

}

this is the function in my service.ts file

  detailKejadian(idkejadian):Observable<any> {

    return this.http.get("http://localhost/matawarga/detailkejadian.php?id=idkejadian");

  }
}

Solution

You are not passing the argument in your detailKejadian method, this string you use in this.http.get is always the same.

You could concatenate the value like this:

detailKejadian(idkejadian):Observable<any> {

    return this.http.get(`http://localhost/matawarga/detailkejadian.php?id=${idkejadian}`);

}

Notice the use of backticks () instead of apostrophes or double quotes, and the template literal${idkejadian}`.

You could also configure the http request.

Answered By – Sébastien

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published