Routen zwischen gemeinsam genutzten Dienst undefiniertDaten mit Winkel 2
export const SchoolyearsRoutes: RouterConfig = [
{ path: '', terminal: true, redirectTo: '/schoolyears' },
{
path: 'schoolyears', component: SchoolyearsRootComponent, children: [
{ path: '', component: SchoolyearsListComponent },
{ path: 'edit/:id', component: SchoolyearsEditComponent },
{ path: 'create', component: SchoolyearsCreateComponent }
]
}];
Dienst:
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Schoolyear } from './schoolyear';
@Injectable()
export class SchoolyearsEditService {
public schoolyear: Schoolyear;
constructor() { }
}
Stammkomponente:
import { Component } from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';
import { SchoolyearsEditService } from '../schoolyears-edit.service';
@Component({ template:`<router-outlet></router-outlet>`, providers: [SchoolyearsEditService]})
export class SchoolyearsRootComponent { }
Kinder Komponente:
import { Component, OnInit } from '@angular/core';
import { RouterConfig, Router, ActivatedRoute} from '@angular/router';
import { SchoolyearsEditService } from '../schoolyears-edit.service';
import { Schoolyear } from '../schoolyear';
@Component({
moduleId: module.id,
selector: 'app-schoolyears-edit',
templateUrl: 'schoolyears-edit.component.html',
styleUrls: ['schoolyears-edit.component.css'],
})
export class SchoolyearsEditComponent implements OnInit {
schoolyear: Schoolyear;
constructor(private route: ActivatedRoute, private router: Router, private schoolyearsService: SchoolyearsEditService) { }
ngOnInit() {
this.schoolyear = this.schoolyearsService.schoolyear;
};
}
HTML:
schoolyear Id:
<input [value]="schoolyear.id">
schoolyear name:
<input [value]="schoolyear.name">
schoolyear start date:
<input [value]="schoolyear.startDate">
schoolyear end date:
<input [value]="schoolyear.endDate">
im html Schuljahr ist immer undefiniert.
Warum werden die Daten nicht korrekt freigegeben?
Ich stelle den Dienst in der übergeordneten Komponente und teilen Sie es in der untergeordneten Komponente. Es sollte funktionieren, aber nicht!
Ich sehe nicht, dass Sie Werte an öffentlichen "Schooleear: Schooleear" Variablen in Ihrem Dienst irgendwo zuweisen. Also sollte es offensichtlich undefiniert sein, oder? – HarshaXsoad
siehe meinen Kommentar zu Günter am Ende. – Elisabeth