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
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.