Ich möchte eine ID von einem bestimmten Produkt durch die URL abrufen. Dies ist mein Code so weit:Angular 2/ASP.NET Core-Abruf-ID von URL
Importe
//Imports from @Angular
import { Component, OnInit } from '@angular/core';
import { RouterModule, Router, Params, RouterLink, ActivatedRoute } from '@angular/router';
import { Http } from '@angular/http';
Konstruktor
constructor(
private _hatListService: HatListService, private _router: Router, private _params: ActivatedRoute) {
}
Hut-editor.component ngOnInit
ngOnInit() {
this._params.params.subscribe((params: Params) => {
var id = params['id'] * 1;//*1 efter det som man vill parse:s so bilr det som int.parse
this._hatListService.getHat(this.id).subscribe(
foundHats => this.thisHatModel = foundHats);
console.log(this.thisHatModel)
});
}
Service
public _getHatById: string = '/api/Hats/GetHatById';
getHat(id: number): Observable<Hat> {
//debugger
return this._http.get(this._getHatById + '/' + id).map(res => <Hat>res.json()).catch(this.handleError);
}
Controller
// GET: api/Hats/5 [HttpGet("GetHatById/{id}")] public async Task<IActionResult> GetHat([FromRoute] int id) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var hat = await _context.Hat.SingleOrDefaultAsync(m => m.Id == id); if (hat == null) { return NotFound(); } return Ok(hat); }
Wenn ich über jedes Produkt schweben wird die URL erzeugt:
Aber, wenn ich das Produkt klicken Sie auf die ID nicht vom Browser gefangen:
contoller:
ich den Fehler bin zu raten, tritt auf dieser Linie in meinem getHat () Methode. Die * 1 ist für die ID auf eine Zahl Gießen:
> this._params.params.subscribe((params: Params) => { var id = params['id'] * 1; });
Ich versuchte, die Angualr.io exapmlpe aber konnte es nicht für mich zu arbeiten. Danke für all die Hilfe