【发布时间】:2019-12-29 00:19:04
【问题描述】:
我创建了一个 Angular 8 应用程序,允许用户从 Angular 材料日期选择器控件中选择一个日期。然后,该应用程序使用 NodeJS 向 Mongodb 发送一个请求,以返回可用的每日会话列表。我的应用程序的这一部分工作正常。返回的每日会话在 html 中动态显示为按钮。我的代码遍历可用会话列表,并使用角度 *ngFor 循环在 html 中创建一个按钮。我的问题是,当用户想要从日期选择器中选择另一个日期时,会返回新的会话列表,并且新的可用日期列表会使用 *ngFor 循环在 html 中创建更多按钮,即附加新的按钮列表到现有的按钮列表。我真正想要的是要从 DOM 中删除的 HTML 中先前显示的按钮以及要在其位置显示的新选择日期的新按钮列表。有人知道怎么做这个吗?谢谢!
下面是我的组件 ts 文件:-
import { Component, OnInit, ViewChild, ElementRef, Renderer2 } from '@angular/core';
import { Tank } from '../../models/tank.model';
import { FormGroup, FormControl } from '@angular/forms';
import * as moment from 'moment';
import { TankService } from 'src/app/services/tank.service';
@Component({
selector: 'app-booking',
templateUrl: './booking.component.html',
styleUrls: ['./booking.component.css']
})
export class BookingComponent implements OnInit {
selectedDate: any;
convertedDate: string;
tankAvailability: Tank[];
dateSelectionButtonPressed = false;
tankOneAvailability: string[] = [];
bookingForm: FormGroup;
constructor(private tankService: TankService) { }
ngOnInit(): void {
this.bookingForm = new FormGroup({
enteredDate: new FormControl(),
});
}
onSelect(event) {
this.selectedDate = event;
this.convertedDate = moment(event).format('DD-MM-YYYY');
this.bookingForm.patchValue({
enteredDate: this.convertedDate
});
}
getTankAvailabilityByDate() {
this.dateSelectionButtonPressed = false;
this.tankAvailability = [];
if (this.convertedDate) {
this.tankService.getTankAvailabilityByDate(this.convertedDate)
.subscribe(tanks => {
this.tankAvailability = tanks.tanks;
this.populateAvailableSessions();
});
}
}
populateAvailableSessions() {
this.dateSelectionButtonPressed = true;
for(let i = 0; i < this.tankAvailability.length; i++) {
if (this.tankAvailability[i].tankNumber === 1) {
console.log(this.tankAvailability[i]);
if (this.tankAvailability[i].sessionOne === false) {
this.tankOneAvailability.push('07:00');
}
if (this.tankAvailability[i].sessionTwo === false) {
this.tankOneAvailability.push('09:00');
}
if (this.tankAvailability[i].sessionThree === false) {
this.tankOneAvailability.push('11:00');
}
if (this.tankAvailability[i].sessionFour === false) {
this.tankOneAvailability.push('13:00');
}
if (this.tankAvailability[i].sessionFive === false) {
this.tankOneAvailability.push('15:00');
}
console.log(this.tankOneAvailability);
}
}
}
}
下面是我的组件 HTML 文件:-
<mat-card>
<div class="center-section">
<h1>Book a float</h1>
<p>
Select a date on the calendar below and we will display all the available float times
</p>
<form (submit)="getTankAvailabilityByDate()" [formGroup]="bookingForm">
<div class="calendar-wrapper">
<mat-calendar [selected]="selectedDate" (selectedChange)="onSelect($event)"></mat-calendar>
</div>
<mat-form-field class="invisible-field">
<input matInput formControlName="enteredDate">
</mat-form-field>
<button mat-raised-button color="primary" type="submit">Get availability</button>
</form>
</div>
</mat-card>
<mat-card *ngIf="dateSelectionButtonPressed">
<mat-card-header>
<mat-card-title>Tank 1</mat-card-title>
</mat-card-header>
<mat-card-content *ngFor="let session of tankOneAvailability">
<button mat-raised-button color="primary">{{session}}</button>
</mat-card-content>
</mat-card>
【问题讨论】:
标签: html angular angular-material mean