I'm trying to pass an object to another component.
Request-list.html
<tr [routerLink]="['Request', { id: request.id }]" *ngFor="let request of requests" [req]="request">
<td class=" ">{{ request.datum }}</td>
<td class=" ">{{ request.artist.name }} </td>
<td class=" ">{{ request.artist.user.first_name }}
<td class=" ">{{ request.user.first_name }}</td>
<td class=" ">{{ request.duration.price }}</td>
<td class=" ">{{ request.created_at }}</td>
<td class=" ">{{ request.rejected_at }}</td>
</tr>
RequestListComponent
import {Component} from '@angular/core';
import {Service} from './../services/service.service';
import {Request} from './../classes/request';
import {RouteConfig, ROUTER_DIRECTIVES} from '@angular/router-deprecated';
import {RequestComponent} from './request.component';
@Component({
directives: [ROUTER_DIRECTIVES, RequestComponent],
templateUrl: 'partials/request-list.html'
})
export class RequestListComponent {
requests: Request[];
constructor(private _Service: Service) {}
ngOnInit(){
this._Service.getRequests().subscribe(data => this.requests = data);
}
}
RequestComponent
import {Component, Input} from '@angular/core';
import {Service} from './../services/service.service';
import {Request} from './../classes/request';
@Component({
templateUrl: 'partials/request.html'
})
export class RequestComponent {
@Input() req: Request;
constructor(private _Service: Service) {}
}
However I get the error: "Can't bind to 'req' since it isn't a known native property"
What am I doing wrong?
As I mentioned in my comment, you cannot use @Input()
if your RequestComponent
is loaded with the Router
. Unfortunately it is not possible to pass RouteData
with the RouteLink
-Directive.
To solve this problem, I would load the Request
inside the RequestComponent
from the service, which you used to load the list of Requests
. To reduce the traffic to a backend, if you have one, you can build some caching mechanism.
export class RequestComponent {
req: Request;
constructor(private _Service: Service, routeParams : RouteParams) {
this.req = _Service.getRequestById(routeParams.get('id'));
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.