Hier ist die Fehlermeldung in der Entwickler-KonsoleChrome „RESTAURANT_NAME“ ist nicht definiert, sondern auf der Seite tatsächlich zeigt den Wert
ERROR TypeError: Cannot read property 'restaurant_name' of undefined at Object.eval [as updateRenderer] (RestaurantDetailComponent.html:2) at Object.debugUpdateRenderer [as updateRenderer] (core.es5.js:13094) at checkAndUpdateView (core.es5.js:12241) at callViewAction (core.es5.js:12601) at execComponentViewsAction (core.es5.js:12533) at checkAndUpdateView (core.es5.js:12242) at callViewAction (core.es5.js:12601) at execEmbeddedViewsAction (core.es5.js:12559) at checkAndUpdateView (core.es5.js:12237) at callViewAction (core.es5.js:12601) ...
Aber in meiner Detailseite, wird der Wert in restaurant_name
tatsächlich beschwert angezeigt . Hier
ist der Code für Detailseite Implementierung: Datei: Restaurant-detail.component.ts
import { Component, OnInit, Input } from '@angular/core';
import { Restaurant } from '../restaurant';
import { RestaurantService } from '../restaurant.service';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-restaurant-detail',
templateUrl: './restaurant-detail.component.html',
styleUrls: ['./restaurant-detail.component.css']
})
export class RestaurantDetailComponent implements OnInit {
id: Number;
sub: any;
@Input() restaurant: Restaurant;
constructor(private restaurantService: RestaurantService, private route: ActivatedRoute) { }
ngOnInit() {
this.sub = this.route.params.subscribe(params => {
this.id = +params['id']; // (+) converts string 'id' to a number
});
this.restaurantService.getRestaurant(this.id).then(
res => this.restaurant = new Restaurant(res.id, res.restaurant_name, res.description
, res.phone, res.address, res.category));
}
}
Datei: Restaurant-detail.component.html
Restaurant detail page with Id: {{ id }}
<section>
Name: {{ restaurant['restaurant_name'] }}
</section>
Datei: Restaurant- service.ts
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { ActivatedRoute, Params } from '@angular/router';
import { Location } from '@angular/common';
import 'rxjs/add/operator/toPromise';
import { Restaurant } from './restaurant';
@Injectable()
export class RestaurantService {
private restaurantUrl = 'api/restaurant'; // URL to web API
constructor(private http: Http) { }
//Get all restaurants
getRestaurants(): Promise<Restaurant[]> {
return this.http.get(this.restaurantUrl)
.toPromise()
.then(response => response.json().
map(x => new Restaurant(x.id, x.restaurant_name, x.description, x.phone, x.address,
x.category)))
.catch(this.handleError);
}
getRestaurant(id: Number):Promise<Restaurant>{
return this.http.get(this.restaurantUrl + "/" + id)
.toPromise()
.then(response => response.json());
}
private handleError(error: any): Promise<any> {
console.error('Error', error);
return Promise.reject(error.message || error);
}
}
Ich benutze Spring Boot als meine Backend abrufen die Daten. Momentan scheint es zu funktionieren, aber ich möchte wissen, warum ich diesen Fehler bekommen habe und wie ich ihn beheben kann, danke.
Voll Github Code: https://github.com/zhengye1/Eatr/tree/dev
Das sollte es tun - 'Name: {{restaurant? .restaurant_name}}' –