简体   繁体   English

Angular2输入/输出在formControl而不是ngModel中

[英]Angular2 Input/Output in formControl instead ngModel

I have a calender directive where I am passing date and want it to update parent component's date too. 我有一个日历指令,我要在其中传递日期,并希望它也更新父组件的日期。 Its working good with ngModel but when trying with formControl, its not updating parent's component data. 它可以与ngModel配合使用,但是在尝试使用formControl时,不会更新父级的组件数据。 Below is code snippet: 下面是代码片段:

// Model value is updating:
<input [(ngModel)]="data" />
<calender [(currentDate)]="currentDate"  ></calender>

//  FormControl value is not updating:
<form [formGroup]="myForm">
<input formControlName="date" />
<calender [(currentDate)]="myForm.controls.date.value"  ></calender>`
</form>

Calender Component's code snippet: 日历组件的代码段:

@Input() currentDate:string;
@Output() currentDateChange: EventEmitter<string> = new EventEmitter<string>();
updateOutput(){ this.currentDate="someDate" this.currentDateChange.emit(this.currentDate); }

You don't need the output. 您不需要输出。 The Input will keep the parent date up to date if you update it with [(ngModel)] 如果您使用[(ngModel)]更新了输入,则输入将使父日期保持最新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM