从Angular中的Select选项更改值

时间:2019-02-28 10:18:18

标签: angular html5

我要完成的是更改StartHour的值(当startHour高于EndHour的值时)EndHour Value必须更改为StartHour加一小时。

<div class="startHourSelection">
    <label *ngIf="startHourSelection">Begin uur</label>
        <div *ngIf="startHourSelection" class="select">    
            <select id="starthour" (change)="onSelectStartHour($event.target.value)" [disabled]="parentIsLoading">
                <option
                    *ngFor="let Hour of startHours; let i = index"
                    value="{{Hour}}"
                    [attr.selected]="i == 0 ? true : null">
                    {{Hour}}
                </option>
            </select>
        </div>
</div>
<div class="endHourSelection">
    <label *ngIf="endHourSelection">Eind uur</label>
        <div *ngIf="endHourSelection" class="select"> 
            <select id="endHour" (change)="onSelectEndHour($event.target.value)" [disabled]="parentIsLoading">
                <option
                    *ngFor="let Hour of endHours; let i = index"
                    value="{{Hour}}"
                    [attr.selected]="i == 0 ? true : null">
                    {{Hour}}
                </option>
            </select>
        </div>
</div>

组件:

两者的@Input都为false,我也知道我所有的时间都是字符串,而不是DATE,我不确定是否要转换它

@Output() selectStartHour = new EventEmitter<string>(); // when a strathour is selected
@Output() selectEndHour = new EventEmitter<string>(); // when a strathour is selected

startHours = [
    '00:00', '01:00', '02:00', '03:00', '04:00', '05:00', '06:00', '07:00', '08:00', '09:00', '10:00', '11:00', '12:00',
    '13:00', '14:00',  '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00'
];

endHours = [
    '01:00', '02:00', '03:00', '04:00', '05:00', '06:00', '07:00', '08:00', '09:00', '10:00', '11:00', '12:00',
    '13:00', '14:00',  '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00', '24:00'

];

onSelectStartHour(startHour: string) {
        this.selectStartHour.emit(startHour);
    }

    onSelectEndHour(endHour: string) {
        this.selectEndHour.emit(endHour);
    }

1 个答案:

答案 0 :(得分:0)

HTML文件更改

在两个选择元素中添加ngModel

例如:

开始时间选择:<select id="starthour" .... [(ngModel)]="startHour">

结束时间选择:<select id="endHour" .... [(ngModel)]="endHour">

TS文件更改

通过onSelectStartHour()方法添加条件代码并更改endHours的值。