簡體   English   中英

EventEmitter不會將值釋放回表單

[英]EventEmitter not emitting value back to form

我正在嘗試從表單獲取更新的日期值,但是它不起作用:

@Component({
    selector: 'date-picker',
    template:
    `   
        <select [(ngModel)]="day" name="dayPicker" class="form-control" style="width: 80px" required>
            <option *ngFor="let day of days" [value]="day">{{day}}</option>
        </select>
        <select [(ngModel)]="month" name="monthPicker" class="form-control" style="width: 80px" required>
            <option *ngFor="let x of months" [value]="x.MonthId">{{x.MonthName}}</option>
        </select>
        <select [(ngModel)]="year" name="yearPicker" class="form-control" style="width: 100px" required>
            <option *ngFor="let year of years" [value]="year">{{year}}</option>
        </select>
    `,
    providers: [
        {
            provide: NG_VALUE_ACCESSOR,
            useExisting: forwardRef(() => DatePicker),
            multi: true,
        }
    ]
})

export class DatePicker implements ControlValueAccessor, OnChanges, OnInit  {

public _day: number = null;
public _month: number = null;
public _year: number = null;

@Input() public inputDate: Date;
@Input() public yearBegin: number;
@Input() public yearEnd: number;

@Output() public dateChange: EventEmitter<Date> = new EventEmitter<Date>();

private months: Month[];
private years: number[];
private daysInMonth: DaysInMonth[];
private days: number[];

private onChangeCallback: (_: any) => {};
private propagateChange = (_: any) => {};

所以當我說改變年份:

set year(val: number) {
    this._year = val;
    if(this._day != null && this._month != null) {
        this.inputDate = new Date(this._year, (this._month - 1), this._day);
        this.dateChange.emit(this.inputDate);
        this.propagateChange(this.inputDate);
    }
}

這是我的用法:

<form #personalInfoForm="ngForm" (ngSubmit)="onSubmit()" class="form-width">
<date-picker [(inputDate)]="parsedDate" name="BirthDate" [yearBegin]="1950" [yearEnd]="2020"> </date-picker>
</form>

當我在下拉列表中更改日期值並按表單的提交按鈕時,我看不到更新的日期。 這是我的自定義日期選擇器控件初始化的同一天。

我看不到有什么東西在監聽那個EventEmitter。

您需要類似(dateChange)="onDateChange($event.value)"其中onDateChange($event.value)是父componenet中的一種接受新值的方法。

角度文檔中有一個很好的簡單示例: https : //angular.io/guide/component-interaction#parent-listens-for-child-event

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM