Good morning, I have problem when i send data through routerLink; in the destination component they are always undefined.
In fact the template is:
<div class="container-fluid">
<form #f="ngForm" (ngSubmit)="onSubmit(f)" novalidate>
<div class="lig">
<md-form-field>
<input mdInput type="number" [(ngModel)]="candidate.orderNumber" name="on" (focusout)="checkForm();" placeholder="Order Number" required>
</md-form-field>
</div>
<div class="lig">
<md-form-field>
<input mdInput [(ngModel)]="candidate.dateOfBirth" [mdDatepicker]="myDatepicker" (focusout)="checkForm();" name="myDate1" placeholder="Birth Date" required>
<md-datepicker-toggle mdSuffix [for]="myDatepicker" ></md-datepicker-toggle>
<md-datepicker #myDatepicker></md-datepicker>
</md-form-field>
</div>
<div class="lig">
<input type="submit" name="enter" value="Enter" >
<button type="button" *ngIf="valid2==1"
[routerLink]="['/reportregistration', candidate.name, candidate.surname, candidate.gender ]"
>PRINT FORM!</button>
</div>
</form>
</div>
From the orderNumber and dateOfBirth of a given candidate I check if it exits then I display more details concerning the candidate.
The problem is that even when the candidate is found (I see it trhough console.log(candidate)) the params candidate.name, candidate.surname, candidate.gender of [routerLink]="['/reportregistration', candidate.name, candidate.surname, candidate.gender ]"
are all undefined in the component reportregistration.
The associated Typescript file is:
import { Component, OnInit } from '@angular/core';
import { NgForm } from "@angular/forms";
import { Response } from "@angular/http";
import { Candidate } from "../candidate.interface";
import { UserService } from "../user.service";
@Component({
selector: 'app-download-registration',
templateUrl: './download-registration.component.html',
styleUrls: ['./download-registration.component.css']
})
export class DownloadRegistrationComponent implements OnInit {
candidate: Candidate;
orderNumber: number;
dateOfBirth:Date;
valid=0;
valid2=0; name;
constructor(private userService: UserService) {
}
ngOnInit() {
this.candidate = {name:"",surname:""};
}
onSubmit(form: NgForm){
this.userService.getCandidate(this.candidate)
.subscribe(
(response: Candidate) => {this.candidate = response,
console.log(this.candidate)
}
);
}
}
And in the service provider I have:
import { Injectable } from "@angular/core";
import { Http, Response, Headers } from "@angular/http";
import 'rxjs/Rx';
import { Observable } from "rxjs";
@Injectable()
export class UserService{
constructor(private http: Http){}
getCandidate(candidate): Observable<any> {
const body = JSON.stringify(candidate);console.log(JSON.stringify(candidate));
const headers = new Headers({'Content-Type': 'application/json'});
return this.http.post('http://127.0.0.1:8000/api/candidate', body, {headers: headers})
.map(
(response: Response) => {
return response.json().candidate
}
);
}
I need some help...
OK. The route is located in the app.module.ts and is:
export const routerConfig =[
{
path:'',
component: WelcomeComponent
},
{
path:'connexion',
component: ConnexionComponent
},
{
path:'connexion/:idcandidates/:name/:surname',
component: ConnexionComponent
},
{
path:'registration',
component:RegistrationComponent
},
{
path:'reportregistration/:name/:surname/:gender/',
component:ReportRegistrationComponent
},
{
path:'viewRegistrations',
component:AllRegistrationComponent
},
{
path:'configs',
component:GeneralConfigsComponent
},
{
path:'downloadRegistration',
component:DownloadRegistrationComponent
},
{
path: "**",
component: WelcomeComponent
}
];