打字稿:获取未定义的值 [英] Typescript: getting an undefined value

查看:33
本文介绍了打字稿:获取未定义的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎有一个我无法解决的问题.

我希望 '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' | 翻译"[(co​​lorPicker)]="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' | 翻译"[(co​​lorPicker)]="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">