angular - 为什么我必须在数据加载之前点击?
问题描述
我的 Kalender 有问题。我想在其中加载一些事件,但我总是必须在它之前单击它。这里有些例子:
(蓝点是事件数组的事件)
我填充事件数组的代码:
initMethod() {
return this.service
.getEmployees()
.pipe(tap(
(listBooks) => {
this.books = listBooks;
this.events = this.books.map((book) => {
return {
start: new Date(book.date_from_og),
end: new Date(book.date_to_og),
type: ""+book.type,
title: "" + book.device + "",
color: colors.blue,
actions: this.actions,
resizable: {
beforeStart: false,
afterEnd: false
},
draggable: false
}
});
}))
}
我的 ngOnInit:
ngOnInit() {
this.initMethod().subscribe(() => {
if(this.events[0].start == this.books[0].date_from_og) {
this.dialog.closeAll();
}
});
}
还有我的构造函数:
constructor(private modal: NgbModal, private service: BookingService, private dialog: MatDialog) {
this.dialog.open(DialogLaedt, {
width: '650px'
});
}
我已经用一个对话框试过了,它会一直显示,直到数据被加载,但这并不奏效。
解决方案
这取决于您用于日历的库,但可能发生在角度区域之外,或者您的更改检测策略设置为OnPush
. 所以你需要告诉 Angular 有些东西已经改变了,它需要刷新视图。为此,您可以使用ChangeDetectorRef
constructor(private modal: NgbModal, private service: BookingService,
private dialog: MatDialog, private cdr: ChangeDetectorRef) {
//...
ngOnInit() {
this.initMethod().subscribe(() => {
//...
this.cdr.detectChanges();//Tell angular that something has changed
推荐阅读
- spring-boot - 为什么应用程序第一次没有启动,而是从第二次启动?
- maven - Maven 无法识别 IntelliJ 项目结构中添加的依赖项
- google-chrome-extension - 当我尝试加载 Google 扩展程序时未显示它
- python - Python FiPy 执行错误,例如 circle.py
- vue.js - Vue.js 元素在 prod 中丢失了它们的 css
- xampp - 我尝试转到我的 phpmyadmin localhost 但 MySQL 说:文档无法连接:无效设置
- excel - 将条形码查找从工作表更改为工作簿
- python - Kivymd 自定义列表对话框。无法在 mddialog 中插入可更改列表
- javascript - 值仅存在于 .then() 主体中
- git - 为什么我在 composer.lock 中有冲突