【发布时间】:2021-11-24 19:40:52
【问题描述】:
我在屏幕上显示通知消息时遇到问题。 当我单击按钮显示通知消息时,会在间隔中定义的几秒钟后显示并删除弹出窗口。但是当我多次单击该按钮时,弹出窗口不会保持可见状态,比如说 2 秒,但只有 1 秒或更少,有时它只会在屏幕上闪烁。
我希望它的行为方式是,当我单击按钮时,将删除任何以前的弹出窗口并显示新的弹出窗口,该窗口将显示 2 秒(除非有下一次单击按钮并显示新的弹出窗口)或旧的弹出窗口将可见 2 秒,新的弹出窗口将放置在前一个/秒的上方。
您有什么更正的建议:
这是我的代码: HTML:
<div class="container">
<div class="notifications">
<scale-notification-message
variant="success"
opened
@fadeAnimation
*ngIf="successMessage$ | async as successMessage">
{{ successMessage }}
</scale-notification-message>
</div>
<button
type="button"
class="btn"
(click)="onClick()">
Click
</button>
</div>
TS 文件
import { tap } from 'rxjs/operators';
import { Observable } from 'rxjs';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
title = 'notification-messages';
visible: boolean = true;
successMessage$: Observable<string> = this.notificationService.successMessageAction$.pipe(tap(_ => {
setTimeout(() => {
this.notificationService.clearSuccessMessage();
}, 2000)
})
);
constructor(private notificationService: NotificationService) {}
onClick() {
this.notificationService.setSuccessMessage('Button clicked!');
}
}
服务
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class NotificationService {
private successMessageSubject = new Subject<string>();
successMessageAction$ = this.successMessageSubject.asObservable();
setSuccessMessage(message: string) {
this.successMessageSubject.next(message);
}
clearSuccessMessage() {
this.setSuccessMessage('');
}
}
【问题讨论】:
标签: angular notifications popup settimeout