【发布时间】:2019-04-24 10:32:27
【问题描述】:
您好,感谢您的所有回答!这是我根据您的输入更新的解决方案。谢谢!
getTime(apptTime) {
const fields = apptTime.split("-");
const startingTime = this.formatTime(+fields[0]);
const endingTime = this.formatTime(+fields[1]);
return startingTime + " - " + endingTime;
}
formatTime(time) {
if (time < 12) {
return time === 0 ? "12am" : time + "am";
} else {
return time === 12 ? time + "pm" : time - 12 + "pm";
}
}
我有一个字符串数组 => appt_timeslots = ["09-12", "12-15", "15-18", "18-21"] 从后端检索到的内容,我想将其显示为单选按钮,如下所示:
appt.component.html
<ng-container *ngFor="let appt of appt_timeslots" [ngSwitch]="appt">
<label ngbButtonLabel class="btn btn-secondary active btn-radio btn-color">
<input ngbButton type="radio" name="timeslot" value="{{ appt }}"/>
<span *ngSwitchCase="'09-12'">9am - 12pm</span>
<span *ngSwitchCase="'12-15'">12pm - 5pm</span>
<span *ngSwitchCase="'15-18'">3pm - 6pm</span>
<span *ngSwitchCase="'18-21'">6pm - 9pm</span>
</label>
</ng-container>
有没有更好的方法来获取我的时间段,以便我可以显示 am/pm 和破折号,而不是使用 switch case?
这样它也可以满足其他时间段的需求(例如下午 2 点 - 下午 4 点),我不必手动添加另一个开关盒来满足新的需求。
感谢您的帮助!
【问题讨论】:
-
也许是打字稿中的时间映射。使用键 =
appt_timeslots值和值 = 9am - 12pm 字符串制作字典。例如。{"09-12":"9am - 12pm", ...}。那么你可以做 {{dict[appt]}} -
现在有时间,我可以这样做stackblitz.com/edit/angular-ngmodel-form-8hyfqc,如果对您有帮助,请根据您的要求扩展它..
标签: angular string switch-statement