Angular 7中可以使用标签和日期值来处理日期选择和显示。下面是一个包含代码示例的解决方法:
{{ currentDate | date:'yyyy-MM-dd' }}
在上面的示例中,currentDate是组件中的一个日期变量,通过管道将其格式化为yyyy-MM-dd的形式进行显示。
import { Component } from '@angular/core';
@Component({
selector: 'app-date-picker',
templateUrl: './date-picker.component.html',
styleUrls: ['./date-picker.component.css']
})
export class DatePickerComponent {
currentDate: Date;
selectedDate: string;
}
在上面的示例中,currentDate是用于显示当前日期的变量,selectedDate是用于保存用户选择的日期的变量。
在上面的示例中,使用ngModel指令将用户选择的日期绑定到selectedDate变量。
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-date-picker',
templateUrl: './date-picker.component.html',
styleUrls: ['./date-picker.component.css']
})
export class DatePickerComponent implements OnInit {
currentDate: Date;
selectedDate: string;
ngOnInit() {
this.currentDate = new Date();
}
}
在上面的示例中,使用ngOnInit钩子函数初始化currentDate变量为当前日期。
这样,你就可以在Angular 7中使用标签和日期值来处理日期选择和显示了。