将路由器参数(id)传递给ngForm然后传递给事件发送器只有一点困难。这里是我如何获得id - 正如你所看到的第一个console.log工作正常,我可以从路由器获取id。现在,我想这个分配给我的singleOpenHome对象(但得到一个未定义的错误):Angular2 - 将路由器参数作为变量传递给ngForm
@Input() singleOpenHome: OpenHomeModel;
ngOnInit() {
// Get the slug on it from the router
this.route.params.forEach((params: Params) => {
let id = params['propertyID'];
console.log('id on the child is:'+id);
//this.singleOpenHome.propertyID == id; Cannot read property 'propertyID' of undefined
});
}
现在我想这个传递给形式:
<form class="my2" #form="ngForm" (ngSubmit)="handleSubmit(form.value, form.valid)" novalidate></form>
这里是我的事件emmitter:
@Output()
update: EventEmitter<OpenHomeModel> = new EventEmitter<OpenHomeModel>();
constructor(private route: ActivatedRoute) {}
handleSubmit(sinlgeOpenHome: OpenHomeModel, isValid: boolean) {
if (isValid) {
this.update.emit(sinlgeOpenHome);
}
}
任何帮助表示赞赏
您显示的所有代码是否属于同一个组件?如果是,只需将路由参数分配给类属性 - this.id = params ['propertyID']; - 然后您可以从组件或其模板中的任何位置访问该属性。 – AngularChef
试过这个:this.singleOpenHome.propertyID = params ['propertyID'];但我不能显示像{{singleOpenHome.propertyID}}模板不会工作的值。 – rhysclay
我不明白。你为什么不尝试我建议的代码?你显示的所有代码是否属于同一个组件? – AngularChef