Fetching the data

Finally, we need to replace our TO-DO comment with the code that will allow our component to fetch the quiz JSON data from the .NET Core QuizController from the ID GET parameter provided by the current route and store it into a local property. Here it is (new/updated lines are highlighted):

    constructor(private activatedRoute: ActivatedRoute,
private router: Router,
private http: HttpClient,
@Inject('BASE_URL') private baseUrl: string) {

var id = +this.activatedRoute.snapshot.params["id"];
console.log(id);
if (id) {
var url = this.baseUrl + "api/quiz/" + id;

this.http.get<Quiz>(url).subscribe(result => {
this.quiz = result;
}, error => console.error(error));
}
else {
console.log("Invalid id: routing back to home...");
this.router.navigate(["home"]);
}
}

It's worth noting that we also had to inject the baseUrl reference in the constructor, which is required to properly build the Web API endpoint address.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.213.44