打字稿:获取未定义的值
[英] Typescript: getting an undefined value
本文介绍了打字稿:获取未定义的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我似乎有一个我无法解决的问题.
我希望 'dialogTitle' 变量也与标题变量一起显示 startHour 和 startMinute 变量.但是,我似乎在标题之后两次获得标题和未定义".我该怎么办?
event-form.component.ts
import { Component, Inject, ViewEncapsulation } from '@angular/core';从'@angular/forms' 导入 { FormBuilder, FormControl, FormGroup };从'@angular/material' 导入 { MAT_DIALOG_DATA, MatDialogRef };从'@fuse/mat-colors' 导入 { MatColors };从角度日历"导入 { CalendarEvent };从 '../event.model' 导入 { CalendarEventModel };import { locale as eventFormEnglish } from './i18n/en';import { locale as eventFormDutch } from './i18n/nl';从 '../calendar.component' 导入 { FuseCalendarComponent };@成分({选择器:'保险丝日历事件表单对话框',templateUrl : './event-form.component.html',styleUrls : ['./event-form.component.scss'],封装:ViewEncapsulation.None})导出类 FuseCalendarEventFormDialogComponent{事件:日历事件;对话标题:字符串;eventForm:FormGroup;动作:字符串;开始时间:字符串;开始分钟:字符串;结束时间:字符串;结束分钟:字符串;presetColors = MatColors.presets;//显示下拉项开始时间 = [{值:'时间-0',视图值:'7'},{值:'时间-1',视图值:'8'},{值:'时间-2',视图值:'9'},{value: 'time-3', viewValue: '10'},{值:'时间-4',视图值:'11'},{值:'时间-5',视图值:'12'},{值:'时间-6',视图值:'13'},{值:'时间-7',视图值:'14'},{value: 'time-8', viewValue: '15'},{值:'时间-9',视图值:'16'},{值:'时间-10',视图值:'17'},{值:'时间-11',视图值:'18'},{值:'时间-12',视图值:'19'},{value: 'time-13', viewValue: '20'},{value: 'time-14', viewValue: '21'},{value: 'time-15', viewValue: '22'},{值:'时间-16',视图值:'23'}];开始时间分钟 = [{值:'时间-0',视图值:'0'},{值:'时间-1',视图值:'5'},{value: 'time-2', viewValue: '10'},{value: 'time-3', viewValue: '15'},{value: 'time-4', viewValue: '20'},{值:'时间-5',视图值:'25'},{value: 'time-6', viewValue: '30'},{值:'时间-7',视图值:'35'},{value: 'time-8', viewValue: '40'},{值:'时间-9',视图值:'45'},{value: 'time-10', viewValue: '50'},{值:'时间-11',视图值:'55'}];结束时间 = [{值:'时间-0',视图值:'7'},{值:'时间-1',视图值:'8'},{值:'时间-2',视图值:'9'},{value: 'time-3', viewValue: '10'},{值:'时间-4',视图值:'11'},{值:'时间-5',视图值:'12'},{值:'时间-6',视图值:'13'},{值:'时间-7',视图值:'14'},{值:'时间-8',视图值:'15'},{值:'时间-9',视图值:'16'},{值:'时间-10',视图值:'17'},{值:'时间-11',视图值:'18'},{值:'时间-12',视图值:'19'},{value: 'time-13', viewValue: '20'},{value: 'time-14', viewValue: '21'},{value: 'time-15', viewValue: '22'},{值:'时间-16',视图值:'23'}];结束时间分钟 = [{值:'时间-0',视图值:'0'},{值:'时间-1',视图值:'5'},{value: 'time-2', viewValue: '10'},{value: 'time-3', viewValue: '15'},{value: 'time-4', viewValue: '20'},{值:'时间-5',视图值:'25'},{value: 'time-6', viewValue: '30'},{值:'时间-7',视图值:'35'},{value: 'time-8', viewValue: '40'},{值:'时间-9',视图值:'45'},{value: 'time-10', viewValue: '50'},{值:'时间-11',视图值:'55'}];构造函数(public dialogRef: MatDialogRef,@Inject(MAT_DIALOG_DATA) 私有数据:任何,私有表单构建器:FormBuilder){this.event = data.event;this.action = data.action;if ( this.action === '编辑' ){this.dialogTitle = this.event.title + " - " + this.startHour + ":" + this.startMinute;}别的{this.dialogTitle = '新事件';this.event = new CalendarEventModel({开始:data.date,结束:数据.日期});}this.eventForm = this.createEventForm();}创建事件表单(){返回新的表单组({标题 : 新的 FormControl(this.event.title),开始:新的FormControl(this.event.start),结束 : 新的 FormControl(this.event.end),startHour : new FormControl(this.startHour),startMinute : new FormControl(this.startMinute),endHour : new FormControl(this.endHour),endMinute : new FormControl(this.endMinute),allDay : new FormControl(this.event.allDay),颜色 : this.formBuilder.group({主要:新的FormControl(this.event.color.primary),次要:新的FormControl(this.event.color.secondary)}),元:this.formBuilder.group({位置:新表单控件(this.event.meta.location),笔记:新的 FormControl(this.event.meta.notes)})});}}
event-form.component.html
<mat-toolbar matDialogTitle class="mat-accent m-0"><div fxFlex fxLayout="row" fxLayoutAlign="space-between center"><span class="title dialog-title">{{dialogTitle}}</span><button mat-button class="mat-icon-button"(点击)="dialogRef.close()"aria-label="关闭对话框"><mat-icon>关闭</mat-icon>按钮>
</mat-toolbar><div mat-dialog-content class="p-24 m-0" fusePerfectScrollbar><form name="eventForm" [formGroup]="eventForm" class="event-form w-100-p" fxLayout="column" fxFlex><mat-form-field class="w-100-p"><输入垫输入名称=标题"表单控件名称=标题"[占位符]="'FORM.TITLE' | 翻译"需要></mat-form-field><div class="py-16" fxFlex="1 0 auto" fxLayout="row"><mat-slide-toggle名称=全天"formControlName="allDay"班级=先生-24"咏叹调标签=整天">{{ 'FORM.ALLDAY' |翻译 }}</mat-slide-toggle>
<div class="py-16" fxFlex="1 0 auto" fxLayout="column" fxLayout.gt-xs="row" formGroupName="color"><mat-form-field class="mr-sm-24" fxFlex><输入垫输入类=原色输入"名称=原色"表单控件名称=主要"[占位符]="'FORM.PRIMARYCOLOR' | 翻译"[(colorPicker)]="event.color.primary"cpWidth="290px"[cpPresetColors]="预设颜色"[style.background]="event.color.primary"(colorPickerChange)="event.color.primary = $event; eventForm.patchValue({color:{primary:$event}})"/></mat-form-field><mat-form-field fxFlex><输入垫输入类=辅助颜色输入"名称=次要颜色"formControlName="次要"[占位符]="'FORM.SECONDARYCOLOR' | 翻译"[(colorPicker)]="event.color.secondary"cpWidth="290px"[cpPresetColors]="预设颜色"[style.background]="event.color.secondary"(colorPickerChange)="event.color.secondary = $event; eventForm.patchValue({color:{secondary:$event}})"/></mat-form-field>
<div fxFlex="1 0 auto" fxLayout="column" fxLayout.gt-xs="row"><mat-form-field class="mr-sm-24" fxFlex><input matInput [matDatepicker]="startDatePicker" [placeholder]="'FORM.STARTDATE' | translate"名称=开始"formControlName="start" required><mat-datepicker-toggle matSuffix [for]="startDatePicker"></mat-datepicker-toggle><mat-datepicker #startDatePicker></mat-datepicker></mat-form-field><mat-form-field><mat-select [placeholder]="'FORM.STARTHOUR' | translate" formControlName="startHour" required><mat-option *ngFor="let startTimeHour of startTimeHours" [value]="startTimeHour">{{startTimeHour.viewValue}}</mat-option></mat-select></mat-form-field><mat-form-field><mat-select [placeholder]="'FORM.STARTMINUTE' | translate" formControlName="startMinute" required><mat-option *ngFor="让 startTimeMinute of startTimeMinutes" [value]="startTimeMinute">{{startTimeMinute.viewValue}}</mat-option></mat-select></mat-form-field>
<div fxFlex="1 0 auto" fxLayout="column" fxLayout.gt-xs="row"><mat-form-field class="mr-sm-24" fxFlex><input matInput [matDatepicker]="endDatePicker" [placeholder]="'FORM.ENDDATE' | translate"名称=结束"formControlName="end" required><mat-datepicker-toggle matSuffix [for]="endDatePicker"></mat-datepicker-toggle><mat-datepicker #endDatePicker></mat-datepicker></mat-form-field><mat-form-field><mat-select [placeholder]="'FORM.ENDHOUR' | translate" formControlName="endHour" required><mat-option *ngFor="let endTimeHour of endTimeHours" [value]="endTimeHour">{{endTimeHour.viewValue}}</mat-option></mat-select></mat-form-field><mat-form-field><mat-select [placeholder]="'FORM.ENDMINUTE' | translate" formControlName="endMinute" required><mat-option *ngFor="let endTimeMinute of endTimeMinutes" [value]="endTimeMinute">{{endTimeMinute.viewValue}}</mat-option></mat-select></mat-form-field>
<mat-form-field formGroupName="meta" class="w-100-p"><输入垫输入名称=位置"表单控件名称=位置"[占位符]="'FORM.LOCATION' | 翻译"></mat-form-field><mat-form-field formGroupName="meta" class="w-100-p">
<div mat-dialog-actions class="m-0 p-16" fxLayout="row" fxLayoutAlign="space-between center"><button *ngIf="action !=='edit'"垫高按钮(点击)="dialogRef.close(eventForm)"class="save-button mat-accent"[禁用]="eventForm.invalid"咏叹调标签=保存">节省按钮><button *ngIf="action ==='edit'"垫高按钮(click)="dialogRef.close(['save',eventForm])"class="save-button mat-accent"[禁用]="eventForm.invalid"咏叹调标签=保存">{{ 'FORM.SAVE' |翻译 }}按钮><button *ngIf="action ==='edit'"垫按钮class="mat-icon-button"(click)="dialogRef.close(['delete',eventForm])"咏叹调标签=删除"matTooltip="删除"><mat-icon>删除</mat-icon>按钮>
我相信这与我没有以正确的方式或类似的方式声明两个变量有关,但我有点新,所以我不确定我在这里做错了什么.是顺序吗?
它应该从数据库条目中获取 startHour 和 startMinute 变量,但我不知道如何.数据库在另一个文件中声明:
calendar.component.ts
import { Component, OnInit, ViewEncapsulation } from '@angular/core';从@angular/forms"导入{FormGroup};从rxjs/主题"导入{主题};从'angularfire2/firestore'导入{ AngularFirestore, AngularFirestoreCollection, AngularFirestoreDocument };从 'rxjs/Observable' 导入 { Observable };从 '@angular/material' 导入 { MatDialog, MatDialogRef };import { startOfDay, isSameDay, isSameMonth } from 'date-fns';从角度日历"导入 { CalendarEvent, CalendarEventAction, CalendarEventTimesChangedEvent, CalendarMonthViewDay };从'@fuse/components/confirm-dialog/confirm-dialog.component' 导入 { FuseConfirmDialogComponent };从@fuse/animations"导入{熔断动画};import { FuseCalendarEventFormDialogComponent } from './event-form/event-form.component';从 './event.model' 导入 { CalendarEventModel };导入 'rxjs/add/operator/map';出口接口 Ride { allDay: boolean;颜色:对象;主要:字符串;次要:字符串;结束:字符串;元:对象;位置:字符串;笔记:字符串;开始:字符串;标题:字符串;}导出接口 RideId 扩展 Ride { id: string }@成分({选择器:'保险丝日历',templateUrl : './calendar.component.html',styleUrls : ['./calendar.component.scss'],封装:ViewEncapsulation.None,动画:熔断动画})导出类 FuseCalendarComponent 实现 OnInit{视图:字符串;查看日期:日期;事件:CalendarEvent[];语言环境:字符串 = 'nl';公共操作:CalendarEventAction[];activeDayIsOpen:布尔值;刷新:主题<任何>= 新主题();对话引用:任何;ConfirmDialogRef: MatDialogRef;selectedDay:任意;游乐设施:可观察的;骑行:可观察的<骑行>;私人骑行集合:AngularFirestoreCollection;私人骑行文档:AngularFirestoreDocument<Ride>;车辆 = [{value: 'vehicle-0', viewValue: 'Test 1'},{value: 'vehicle-1', viewValue: 'Test 2'}]构造函数(公共对话:MatDialog,私有只读数据库:AngularFirestore,){this.view = '月';this.viewDate = 新日期();this.activeDayIsOpen = true;this.selectedDay = {date: startOfDay(new Date())};this.actions = [{标签:'<i class="material-icons s-16">edit</i>',onClick: ({event}: { event: CalendarEvent }): void =>{this.editEvent('edit', 事件);}},{标签:'<i class="material-icons s-16">删除</i>',onClick: ({event}: { event: CalendarEvent }): void =>{this.deleteEvent(事件);}}];/*** 从服务/服务器获取事件*/this.ridesCollection = db.collection('rides');this.rides = this.ridesCollection.snapshotChanges().map(actions => {返回 actions.map(a => {const data = a.payload.doc.data() as Ride;const id = a.payload.doc.id;返回 { id, ...数据};});});//this.setEvents();}ngOnInit(){/*** 观看重新渲染刷新以更新数据库*///this.refresh.subscribe(updateDB => {////console.warn('REFRESH');//如果(更新数据库)//{////console.warn('UPDATE DB');//this.calendarService.updateEvents(this.events);//}//});//this.calendarService.onEventsUpdated.subscribe(events => {//this.setEvents();//this.refresh.next();//});//this.ridesDocument = this.db.doc('rides/id');//this.ride = this.ridesDocument.valueChanges();}设置事件(){//设置this.events//this.events = this.ridesCollection.add(rides);}/*** 在视图渲染器之前* @param {any} 标头* @param {any} 正文*/beforeMonthViewRender({header, body}){//console.info('beforeMonthViewRender');/*** 获取所选日期*/const _selectedDay = body.find((_day) => {返回 _day.date.getTime() === this.selectedDay.date.getTime();});如果(_selectedDay){/***设置selectedday样式* @type {字符串}*/_selectedDay.cssClass = 'mat-elevation-z3';}}/*** 天点击* @param {MonthViewDay} 天*/dayClicked(day: CalendarMonthViewDay): 无效{const 日期:日期 = day.date;const 事件:CalendarEvent[] = day.events;if ( isSameMonth(date, this.viewDate) ){if ( (isSameDay(this.viewDate, date) && this.activeDayIsOpen === true) || events.length === 0 ){this.activeDayIsOpen = false;}别的{this.activeDayIsOpen = true;this.viewDate = 日期;}}this.selectedDay = 天;this.refresh.next();}/*** 活动时间已更改* 事件删除或调整大小* @param {CalendarEvent} 事件* @param {Date} newStart* @param {Date} newEnd*/eventTimesChanged({event, newStart, newEnd}: CalendarEventTimesChangedEvent): void{event.start = newStart;event.end = newEnd;//console.warn('Dropped or resized', event);this.refresh.next(true);}/*** 删除事件* @param 事件*/删除事件(骑){this.confirmDialogRef = this.dialog.open(FuseConfirmDialogComponent, {禁用关闭:假});this.confirmDialogRef.componentInstance.confirmMessage = '确定要删除吗?';this.confirmDialogRef.afterClosed().subscribe(result => {如果(结果){//const eventIndex = this.events.indexOf(event);//this.events.splice(eventIndex, 1);//this.refresh.next(true);}this.confirmDialogRef = null;});}/*** 编辑事件* @param {string} 动作* @param {CalendarEvent} 事件*/编辑事件(动作:字符串,事件:日历事件){//const eventIndex = this.events.indexOf(event);this.dialogRef = this.dialog.open(FuseCalendarEventFormDialogComponent, {panelClass: '事件表单-对话框',数据 : {事件:事件,行动:行动}});//console.log(事件);this.dialogRef.afterClosed().订阅(响应=> {如果(!响应){返回;}常量动作类型:字符串=响应[0];const formData: FormGroup = response[1];开关(动作类型){/*** 节省*/案例保存":var saveData: 任何;//this.events[eventIndex] = Object.assign(this.events[eventIndex], formData.getRawValue());this.refresh.next(true);saveData = formData.getRawValue();//saveData.start.setHours(saveData.startHour);//saveData.start.setMinutes(saveData.startMinute);this.ridesDocument = this.db.doc('rides/' + event.id);this.ridesDocument.set(saveData);休息;/*** 删除*/案例删除"://this.deleteEvent(事件);this.ridesDocument = this.db.doc('rides/' + event.id);this.ridesDocument.delete();休息;}});}/*** 添加事件*/addEvent(): 无效{this.dialogRef = this.dialog.open(FuseCalendarEventFormDialogComponent, {panelClass: '事件表单-对话框',数据 : {动作:'新',日期:this.selectedDay.date}});this.dialogRef.afterClosed().subscribe((响应:FormGroup) => {如果(!响应){返回;}const newEvent = response.getRawValue();//newEvent.actions = this.actions;//this.events.push(newEvent);this.refresh.next(true);this.ridesCollection.add(newEvent);});}}
提前致谢!
解决方案
您似乎没有为 startHour
或 startMinute
赋予任何值.在你有的地方
startHour: 字符串开始分钟:字符串
您只是声明这些变量是字符串类型,而不是给它们一个值.如果你想给他们一个值,你可以在同一点赋值,就像这样:
startHour: string = 'value'开始分钟:字符串 = '值'
或者在你的构造函数中,就在你使用它们之前
this.event = data.event;this.action = data.action;this.startHour = '值';this.startMinute = '值';if ( this.action === '编辑' ){this.dialogTitle = this.event.title + " - " + this.startHour + ":" + this.startMinute;}
希望这有帮助,祝你好运!
I seem to have a problem I can't solve.
I want 'dialogTitle' variable to also display the startHour and startMinute variables alongside the title variable. However, I seem to get the title and 'undefined' twice after the title. What should I do?
event-form.component.ts
import { Component, Inject, ViewEncapsulation } from '@angular/core';
import { FormBuilder, FormControl, FormGroup } from '@angular/forms';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material';
import { MatColors } from '@fuse/mat-colors';
import { CalendarEvent } from 'angular-calendar';
import { CalendarEventModel } from '../event.model';
import { locale as eventFormEnglish } from './i18n/en';
import { locale as eventFormDutch } from './i18n/nl';
import { FuseCalendarComponent } from '../calendar.component';
@Component({
selector : 'fuse-calendar-event-form-dialog',
templateUrl : './event-form.component.html',
styleUrls : ['./event-form.component.scss'],
encapsulation: ViewEncapsulation.None
})
export class FuseCalendarEventFormDialogComponent
{
event: CalendarEvent;
dialogTitle: string;
eventForm: FormGroup;
action: string;
startHour: string;
startMinute: string;
endHour: string;
endMinute: string;
presetColors = MatColors.presets;
//Shows dropdown items
startTimeHours = [
{value: 'time-0', viewValue: '7'},
{value: 'time-1', viewValue: '8'},
{value: 'time-2', viewValue: '9'},
{value: 'time-3', viewValue: '10'},
{value: 'time-4', viewValue: '11'},
{value: 'time-5', viewValue: '12'},
{value: 'time-6', viewValue: '13'},
{value: 'time-7', viewValue: '14'},
{value: 'time-8', viewValue: '15'},
{value: 'time-9', viewValue: '16'},
{value: 'time-10', viewValue: '17'},
{value: 'time-11', viewValue: '18'},
{value: 'time-12', viewValue: '19'},
{value: 'time-13', viewValue: '20'},
{value: 'time-14', viewValue: '21'},
{value: 'time-15', viewValue: '22'},
{value: 'time-16', viewValue: '23'}
];
startTimeMinutes = [
{value: 'time-0', viewValue: '0'},
{value: 'time-1', viewValue: '5'},
{value: 'time-2', viewValue: '10'},
{value: 'time-3', viewValue: '15'},
{value: 'time-4', viewValue: '20'},
{value: 'time-5', viewValue: '25'},
{value: 'time-6', viewValue: '30'},
{value: 'time-7', viewValue: '35'},
{value: 'time-8', viewValue: '40'},
{value: 'time-9', viewValue: '45'},
{value: 'time-10', viewValue: '50'},
{value: 'time-11', viewValue: '55'}
];
endTimeHours = [
{value: 'time-0', viewValue: '7'},
{value: 'time-1', viewValue: '8'},
{value: 'time-2', viewValue: '9'},
{value: 'time-3', viewValue: '10'},
{value: 'time-4', viewValue: '11'},
{value: 'time-5', viewValue: '12'},
{value: 'time-6', viewValue: '13'},
{value: 'time-7', viewValue: '14'},
{value: 'time-8', viewValue: '15'},
{value: 'time-9', viewValue: '16'},
{value: 'time-10', viewValue: '17'},
{value: 'time-11', viewValue: '18'},
{value: 'time-12', viewValue: '19'},
{value: 'time-13', viewValue: '20'},
{value: 'time-14', viewValue: '21'},
{value: 'time-15', viewValue: '22'},
{value: 'time-16', viewValue: '23'}
];
endTimeMinutes = [
{value: 'time-0', viewValue: '0'},
{value: 'time-1', viewValue: '5'},
{value: 'time-2', viewValue: '10'},
{value: 'time-3', viewValue: '15'},
{value: 'time-4', viewValue: '20'},
{value: 'time-5', viewValue: '25'},
{value: 'time-6', viewValue: '30'},
{value: 'time-7', viewValue: '35'},
{value: 'time-8', viewValue: '40'},
{value: 'time-9', viewValue: '45'},
{value: 'time-10', viewValue: '50'},
{value: 'time-11', viewValue: '55'}
];
constructor(
public dialogRef: MatDialogRef<FuseCalendarEventFormDialogComponent>,
@Inject(MAT_DIALOG_DATA) private data: any,
private formBuilder: FormBuilder
)
{
this.event = data.event;
this.action = data.action;
if ( this.action === 'edit' )
{
this.dialogTitle = this.event.title + " - " + this.startHour + ":" + this.startMinute;
}
else
{
this.dialogTitle = 'New Event';
this.event = new CalendarEventModel({
start: data.date,
end : data.date
});
}
this.eventForm = this.createEventForm();
}
createEventForm()
{
return new FormGroup({
title : new FormControl(this.event.title),
start : new FormControl(this.event.start),
end : new FormControl(this.event.end),
startHour : new FormControl(this.startHour),
startMinute : new FormControl(this.startMinute),
endHour : new FormControl(this.endHour),
endMinute : new FormControl(this.endMinute),
allDay : new FormControl(this.event.allDay),
color : this.formBuilder.group({
primary : new FormControl(this.event.color.primary),
secondary : new FormControl(this.event.color.secondary)
}),
meta :
this.formBuilder.group({
location: new FormControl(this.event.meta.location),
notes : new FormControl(this.event.meta.notes)
})
});
}
}
event-form.component.html
<div class="dialog-content-wrapper">
<mat-toolbar matDialogTitle class="mat-accent m-0">
<div fxFlex fxLayout="row" fxLayoutAlign="space-between center">
<span class="title dialog-title">{{dialogTitle}}</span>
<button mat-button class="mat-icon-button"
(click)="dialogRef.close()"
aria-label="Close dialog">
<mat-icon>close</mat-icon>
</button>
</div>
</mat-toolbar>
<div mat-dialog-content class="p-24 m-0" fusePerfectScrollbar>
<form name="eventForm" [formGroup]="eventForm" class="event-form w-100-p" fxLayout="column" fxFlex>
<mat-form-field class="w-100-p">
<input matInput
name="title"
formControlName="title"
[placeholder]="'FORM.TITLE' | translate"
required>
</mat-form-field>
<div class="py-16" fxFlex="1 0 auto" fxLayout="row">
<mat-slide-toggle
name="allDay"
formControlName="allDay"
class="mr-24"
aria-label="All day">
{{ 'FORM.ALLDAY' | translate }}
</mat-slide-toggle>
</div>
<div class="py-16" fxFlex="1 0 auto" fxLayout="column" fxLayout.gt-xs="row" formGroupName="color">
<mat-form-field class="mr-sm-24" fxFlex>
<input matInput
class="primary-color-input"
name="primary color"
formControlName="primary"
[placeholder]="'FORM.PRIMARYCOLOR' | translate"
[(colorPicker)]="event.color.primary"
cpWidth="290px"
[cpPresetColors]="presetColors"
[style.background]="event.color.primary"
(colorPickerChange)="event.color.primary = $event; eventForm.patchValue({color:{primary:$event}})"/>
</mat-form-field>
<mat-form-field fxFlex>
<input matInput
class="secondary-color-input"
name="secondary color"
formControlName="secondary"
[placeholder]="'FORM.SECONDARYCOLOR' | translate"
[(colorPicker)]="event.color.secondary"
cpWidth="290px"
[cpPresetColors]="presetColors"
[style.background]="event.color.secondary"
(colorPickerChange)="event.color.secondary = $event; eventForm.patchValue({color:{secondary:$event}})"/>
</mat-form-field>
</div>
<div fxFlex="1 0 auto" fxLayout="column" fxLayout.gt-xs="row">
<mat-form-field class="mr-sm-24" fxFlex>
<input matInput [matDatepicker]="startDatePicker" [placeholder]="'FORM.STARTDATE' | translate"
name="start"
formControlName="start" required>
<mat-datepicker-toggle matSuffix [for]="startDatePicker"></mat-datepicker-toggle>
<mat-datepicker #startDatePicker></mat-datepicker>
</mat-form-field>
<mat-form-field>
<mat-select [placeholder]="'FORM.STARTHOUR' | translate" formControlName="startHour" required>
<mat-option *ngFor="let startTimeHour of startTimeHours" [value]="startTimeHour">
{{startTimeHour.viewValue}}
</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field>
<mat-select [placeholder]="'FORM.STARTMINUTE' | translate" formControlName="startMinute" required>
<mat-option *ngFor="let startTimeMinute of startTimeMinutes" [value]="startTimeMinute">
{{startTimeMinute.viewValue}}
</mat-option>
</mat-select>
</mat-form-field>
</div>
<div fxFlex="1 0 auto" fxLayout="column" fxLayout.gt-xs="row">
<mat-form-field class="mr-sm-24" fxFlex>
<input matInput [matDatepicker]="endDatePicker" [placeholder]="'FORM.ENDDATE' | translate"
name="end"
formControlName="end" required>
<mat-datepicker-toggle matSuffix [for]="endDatePicker"></mat-datepicker-toggle>
<mat-datepicker #endDatePicker></mat-datepicker>
</mat-form-field>
<mat-form-field>
<mat-select [placeholder]="'FORM.ENDHOUR' | translate" formControlName="endHour" required>
<mat-option *ngFor="let endTimeHour of endTimeHours" [value]="endTimeHour">
{{endTimeHour.viewValue}}
</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field>
<mat-select [placeholder]="'FORM.ENDMINUTE' | translate" formControlName="endMinute" required>
<mat-option *ngFor="let endTimeMinute of endTimeMinutes" [value]="endTimeMinute">
{{endTimeMinute.viewValue}}
</mat-option>
</mat-select>
</mat-form-field>
</div>
<mat-form-field formGroupName="meta" class="w-100-p">
<input matInput
name="location"
formControlName="location"
[placeholder]="'FORM.LOCATION' | translate">
</mat-form-field>
<mat-form-field formGroupName="meta" class="w-100-p">
<textarea matInput
formControlName="notes"
[placeholder]="'FORM.NOTES' | translate"
mat-maxlength="250"
max-rows="4">
</textarea>
</mat-form-field>
</form>
</div>
<div mat-dialog-actions class="m-0 p-16" fxLayout="row" fxLayoutAlign="space-between center">
<button *ngIf="action !=='edit'"
mat-raised-button
(click)="dialogRef.close(eventForm)"
class="save-button mat-accent"
[disabled]="eventForm.invalid"
aria-label="SAVE">
SAVE
</button>
<button *ngIf="action ==='edit'"
mat-raised-button
(click)="dialogRef.close(['save',eventForm])"
class="save-button mat-accent"
[disabled]="eventForm.invalid"
aria-label="SAVE">
{{ 'FORM.SAVE' | translate }}
</button>
<button *ngIf="action ==='edit'"
mat-button
class="mat-icon-button"
(click)="dialogRef.close(['delete',eventForm])"
aria-label="Delete"
matTooltip="Delete">
<mat-icon>delete</mat-icon>
</button>
</div>
</div>
I believe it has to do with the fact I haven't declared the two variables the right way or something like that, but I'm kinda new, so I'm not sure what I'm doing wrong here. Is it the order?
EDIT: It should get the startHour and startMinute variables from the database entries, but I do not know how. The database is declared in another file:
calendar.component.ts
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { Subject } from 'rxjs/Subject';
import { AngularFirestore, AngularFirestoreCollection, AngularFirestoreDocument } from 'angularfire2/firestore';
import { Observable } from 'rxjs/Observable';
import { MatDialog, MatDialogRef } from '@angular/material';
import { startOfDay, isSameDay, isSameMonth } from 'date-fns';
import { CalendarEvent, CalendarEventAction, CalendarEventTimesChangedEvent, CalendarMonthViewDay } from 'angular-calendar';
import { FuseConfirmDialogComponent } from '@fuse/components/confirm-dialog/confirm-dialog.component';
import { fuseAnimations } from '@fuse/animations';
import { FuseCalendarEventFormDialogComponent } from './event-form/event-form.component';
import { CalendarEventModel } from './event.model';
import 'rxjs/add/operator/map';
export interface Ride { allDay: boolean; color: Object; primary: string; secondary: string; end: string; meta: Object; location: string; notes: string; start: string; title: string; }
export interface RideId extends Ride { id: string }
@Component({
selector : 'fuse-calendar',
templateUrl : './calendar.component.html',
styleUrls : ['./calendar.component.scss'],
encapsulation: ViewEncapsulation.None,
animations : fuseAnimations
})
export class FuseCalendarComponent implements OnInit
{
view: string;
viewDate: Date;
events: CalendarEvent[];
locale: string = 'nl';
public actions: CalendarEventAction[];
activeDayIsOpen: boolean;
refresh: Subject<any> = new Subject();
dialogRef: any;
confirmDialogRef: MatDialogRef<FuseConfirmDialogComponent>;
selectedDay: any;
rides: Observable<RideId[]>;
ride: Observable<Ride>;
private ridesCollection: AngularFirestoreCollection<Ride>;
private ridesDocument: AngularFirestoreDocument<Ride>;
vehicle = [
{value: 'vehicle-0', viewValue: 'Test 1'},
{value: 'vehicle-1', viewValue: 'Test 2'}
]
constructor(
public dialog: MatDialog,
private readonly db: AngularFirestore,
)
{
this.view = 'month';
this.viewDate = new Date();
this.activeDayIsOpen = true;
this.selectedDay = {date: startOfDay(new Date())};
this.actions = [
{
label : '<i class="material-icons s-16">edit</i>',
onClick: ({event}: { event: CalendarEvent }): void => {
this.editEvent('edit', event);
}
},
{
label : '<i class="material-icons s-16">delete</i>',
onClick: ({event}: { event: CalendarEvent }): void => {
this.deleteEvent(event);
}
}
];
/**
* Get events from service/server
*/
this.ridesCollection = db.collection<Ride>('rides');
this.rides = this.ridesCollection.snapshotChanges().map(actions => {
return actions.map(a => {
const data = a.payload.doc.data() as Ride;
const id = a.payload.doc.id;
return { id, ...data };
});
});
// this.setEvents();
}
ngOnInit()
{
/**
* Watch re-render-refresh for updating db
*/
// this.refresh.subscribe(updateDB => {
// // console.warn('REFRESH');
// if ( updateDB )
// {
// // console.warn('UPDATE DB');
// this.calendarService.updateEvents(this.events);
// }
// });
// this.calendarService.onEventsUpdated.subscribe(events => {
// this.setEvents();
// this.refresh.next();
// });
// this.ridesDocument = this.db.doc('rides/id');
// this.ride = this.ridesDocument.valueChanges();
}
setEvents()
{
//set this.events
// this.events = this.ridesCollection.add(rides);
}
/**
* Before View Renderer
* @param {any} header
* @param {any} body
*/
beforeMonthViewRender({header, body})
{
// console.info('beforeMonthViewRender');
/**
* Get the selected day
*/
const _selectedDay = body.find((_day) => {
return _day.date.getTime() === this.selectedDay.date.getTime();
});
if ( _selectedDay )
{
/**
* Set selectedday style
* @type {string}
*/
_selectedDay.cssClass = 'mat-elevation-z3';
}
}
/**
* Day clicked
* @param {MonthViewDay} day
*/
dayClicked(day: CalendarMonthViewDay): void
{
const date: Date = day.date;
const events: CalendarEvent[] = day.events;
if ( isSameMonth(date, this.viewDate) )
{
if ( (isSameDay(this.viewDate, date) && this.activeDayIsOpen === true) || events.length === 0 )
{
this.activeDayIsOpen = false;
}
else
{
this.activeDayIsOpen = true;
this.viewDate = date;
}
}
this.selectedDay = day;
this.refresh.next();
}
/**
* Event times changed
* Event dropped or resized
* @param {CalendarEvent} event
* @param {Date} newStart
* @param {Date} newEnd
*/
eventTimesChanged({event, newStart, newEnd}: CalendarEventTimesChangedEvent): void
{
event.start = newStart;
event.end = newEnd;
// console.warn('Dropped or resized', event);
this.refresh.next(true);
}
/**
* Delete Event
* @param event
*/
deleteEvent(ride)
{
this.confirmDialogRef = this.dialog.open(FuseConfirmDialogComponent, {
disableClose: false
});
this.confirmDialogRef.componentInstance.confirmMessage = 'Are you sure you want to delete?';
this.confirmDialogRef.afterClosed().subscribe(result => {
if ( result )
{
// const eventIndex = this.events.indexOf(event);
// this.events.splice(eventIndex, 1);
// this.refresh.next(true);
}
this.confirmDialogRef = null;
});
}
/**
* Edit Event
* @param {string} action
* @param {CalendarEvent} event
*/
editEvent(action: string, event: CalendarEvent)
{
// const eventIndex = this.events.indexOf(event);
this.dialogRef = this.dialog.open(FuseCalendarEventFormDialogComponent, {
panelClass: 'event-form-dialog',
data : {
event : event,
action: action
}
});
// console.log(event);
this.dialogRef.afterClosed()
.subscribe(response => {
if ( !response )
{
return;
}
const actionType: string = response[0];
const formData: FormGroup = response[1];
switch ( actionType )
{
/**
* Save
*/
case 'save':
var saveData: any;
// this.events[eventIndex] = Object.assign(this.events[eventIndex], formData.getRawValue());
this.refresh.next(true);
saveData = formData.getRawValue();
// saveData.start.setHours(saveData.startHour);
// saveData.start.setMinutes(saveData.startMinute);
this.ridesDocument = this.db.doc('rides/' + event.id);
this.ridesDocument.set(saveData);
break;
/**
* Delete
*/
case 'delete':
// this.deleteEvent(event);
this.ridesDocument = this.db.doc('rides/' + event.id);
this.ridesDocument.delete();
break;
}
});
}
/**
* Add Event
*/
addEvent(): void
{
this.dialogRef = this.dialog.open(FuseCalendarEventFormDialogComponent, {
panelClass: 'event-form-dialog',
data : {
action: 'new',
date : this.selectedDay.date
}
});
this.dialogRef.afterClosed()
.subscribe((response: FormGroup) => {
if ( !response )
{
return;
}
const newEvent = response.getRawValue();
// newEvent.actions = this.actions;
// this.events.push(newEvent);
this.refresh.next(true);
this.ridesCollection.add(newEvent);
});
}
}
Thanks in advance!
解决方案
It looks like you haven't given any values to startHour
or startMinute
. Up where you have
startHour: string
startMinute: string
you're simply declaring that these variables are of type string, but not giving them a value. If you want to give them a value, you can either assign a value at the same point, like this:
startHour: string = 'value'
startMinute: string = 'value'
or in your constructor, right before you use them
this.event = data.event;
this.action = data.action;
this.startHour = 'value';
this.startMinute = 'value';
if ( this.action === 'edit' )
{
this.dialogTitle = this.event.title + " - " + this.startHour + ":" + this.startMinute;
}
Hope this helped and good luck!
这篇关于打字稿:获取未定义的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文