英文:
Display a div only for few seconds in Angular
问题
我想在 Angular 应用中仅显示一则通知,持续几秒钟,该事件在点击按钮后触发。
英文:
I want to display a notification on angular app only for few seconds, the event is triggered after clicking a button.
答案1
得分: 1
以下是翻译好的内容:
html
<button (click)="showDiv()">点击</button>
<div *ngIf="!hideDivFlg">我的 Div</div>
.ts
showDiv() {
this.hideDivFlg = false;
setTimeout(() => {
this.hideDivFlg = true;
}, 4000);
}
英文:
how about this:
html
<button (click)="showDiv()">Click</button>
<div *ngIf="!hideDivFlg">My Div</div>
.ts
showDiv() {
this.hideDivFlg = false;
setTimeout(() => {
this.hideDivFlg = true;
}, 4000);
}
答案2
得分: 1
以下是您要翻译的内容:
有一个用于显示提示消息的npm包:
https://www.npmjs.com/package/ngx-toastr
npm install ngx-toastr --save
请检查与Angular版本的兼容性
ngx-toastr | Angular |
---|---|
11.3.3 | 8.x |
12.1.0 | 9.x |
13.2.1 | 10.x 11.x |
14.3.0 | 12.x 13.x |
15.2.2 | 14.x. |
当前版本 | >= 15.x |
npm install @angular/animations --save
app.module.ts
import { ToastrModule } from 'ngx-toastr';
...
imports: [
...
ToastrModule.forRoot(),
...]
styles.css
@import '~ngx-toastr/toastr';
angular.json
"styles": [
...
"node_modules/ngx-toastr/toastr.css"
],
创建一个新的服务
common.service.ts
import { Injectable } from '@angular/core';
import { IndividualConfig, ToastrService } from 'ngx-toastr';
export interface toastPayload {
message: string;
title: string;
ic: IndividualConfig;
type: string;
}
@Injectable({
providedIn: 'root'
})
export class CommonService {
constructor(
private toastr: ToastrService
) { }
showToast(toast: toastPayload) {
this.toastr.show(
toast.message,
toast.title,
toast.ic,
'toast-' + toast.type
);
}
}
最后在您的组件中使用它:
items-component.ts
import { CommonService, toastPayload } from 'src/app/services/common.service';
export class ItemsComponent implements OnInit {
toast!: toastPayload;
constructor(private cs: CommonService,)
}
displayToast(){
this.toast = {
message: '一个酷炫的消息',
title: '一个酷炫的标题',
type: "success",
ic: {
timeOut: 2500,
closeButton: true,
} as IndividualConfig,
};
this.cs.showToast(this.toast);
}
英文:
There is a npm package for toast messages:
https://www.npmjs.com/package/ngx-toastr
npm install ngx-toastr --save
please check the angular version for compatibility
ngx-toastr | Angular |
---|---|
11.3.3 | 8.x |
12.1.0 | 9.x |
13.2.1 | 10.x 11.x |
14.3.0 | 12.x 13.x |
15.2.2 | 14.x. |
current | >= 15.x |
npm install @angular/animations --save
app.module.ts
import { ToastrModule } from 'ngx-toastr';
...
imports: [
...
ToastrModule.forRoot(),
...]
styles.css
@import '~ngx-toastr/toastr';
angular.json
"styles": [
...
"node_modules/ngx-toastr/toastr.css"
],
create a new service
common.service.ts
import { Injectable } from '@angular/core';
import { IndividualConfig, ToastrService } from 'ngx-toastr';
export interface toastPayload {
message: string;
title: string;
ic: IndividualConfig;
type: string;
}
@Injectable({
providedIn: 'root'
})
export class CommonService {
constructor(
private toastr: ToastrService
) { }
showToast(toast: toastPayload) {
this.toastr.show(
toast.message,
toast.title,
toast.ic,
'toast-' + toast.type
);
}
}
Finally use it in your component:
items-component.ts
import { CommonService, toastPayload } from 'src/app/services/common.service';
export class ItemsComponent implements OnInit {
toast!: toastPayload;
constructor(private cs: CommonService,)
}
displayToast(){
this.toast = {
message: 'A cool Message',
title: 'A cool Title',
type: "success",
ic: {
timeOut: 2500,
closeButton: true,
} as IndividualConfig,
};
this.cs.showToast(this.toast);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论