我正在尝试使用服务将表单(mdf)的输入值传递给另一个组件。这是我的服务:
sharedData.service.ts
import { Injectable } from '@angular/core';
@Injectable()
export class SharedDataService {
eventData: any=[];
constructor() {}
setEventData(data: any)
{
this.eventData.push(data);
}
getEventData()
{
return this.eventData;
}
}
在第一个组件中,当我点击一个按钮时,我想将表单值传递给服务:
formData.component.html
<form [formGroup]="eventForm" novalidate (ngSubmit)="eventForm.valid && add(eventForm.value)">
<fieldset>
<input name="title" formControlName="title" type="text">
......
<a [routerLink]="['/preview']" target="_blank" class="btn btn-primary" (click)="preview(eventForm.value)">Preview</a>
......
</fieldset>
</form>
formData.component.ts
preview(eventData: any)
{
this.sharedData.setEventData(eventData);
}
preview.component.ts
ngOnInit() {
this.eventData=this.sharedData.getEventData();
}
但
中没有任何内容preview.component.html
<b>Event Title: </b> {{eventData.title}}