在Angular中仅在几秒钟内显示一个div。

huangapple go评论64阅读模式
英文:

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);
}

在Angular中仅在几秒钟内显示一个div。

英文:

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);
}

在Angular中仅在几秒钟内显示一个div。

huangapple
  • 本文由 发表于 2023年4月20日 00:45:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76056984.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定