Angular PrimeNG消息组件的属性
Angular PrimeNG是一个开源框架,它有丰富的原生Angular UI组件,可以用来做很好的造型,这个框架用来做响应式网站,非常方便。在这篇文章中,我们将学习如何在Angular PrimeNG中使用Messages Properties。我们还将学习这些属性,以及它们在代码中的语法。
信息组件用于显示具有特别严重性的信息。
信息组件的Angular PrimeNG属性:
- value。它是一个要显示的信息数组。它是数组的数据类型,默认值为空。
- closable。它定义了消息框是否可以通过点击图标关闭。它是布尔数据类型,默认值为true。
- style。它设置组件的内联风格。它是字符串数据类型,默认值为空。
- styleClass。它设置组件的风格类。它是字符串数据类型,默认值为空。
- enableService。它指定是否启用显示服务信息。它是布尔数据类型,默认值为真。
- escape:它指定显示的信息是否会被转义。它是一个布尔数据类型,默认值为true。
- key。它是匹配消息关键的Id,以便在基于服务的消息传递中启用范围。它是字符串数据类型,默认值为空。
- showTransitionOptions。它设置显示动画的过渡选项。它的数据类型是布尔型,默认值是300ms缓和。
- hideTransitionOptions。设置隐藏动画的过渡选项。它是布尔数据类型,默认值是200ms cubic-bezier(0.86, 0, 0.07, 1)。
创建Angular应用程序和模块安装。
第1步:使用以下命令创建一个Angular应用程序。
ng new appname
第2步:创建你的项目文件夹即appname后,使用以下命令移动到它。
cd appname
第3步在你给定的目录中安装PrimeNG。
npm install primeng --save
npm install primeicons --save
项目结构:完成安装后,它将看起来像下面这样。
- 要运行上述文件,请运行以下命令。
ng serve --save
例子1:这是一个基本的例子,展示了如何使用Angular PrimeNG消息属性。
- app.component.html:
<h1 style="color: green">GeeksforGeeks</h1>
<h5>PrimeNG Messages Properties</h5>
<p-messages [(value)]="gfg"
[enableService]="false">
</p-messages>
- app.component.ts:
import { Component } from "@angular/core";
import { Message } from "primeng/api";
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
})
export class AppComponent {
gfg: Message[];
ngOnInit() {
this.gfg = [
{ detail: "This is a message", severity: "warn" },
{ detail: "This is a message", severity: "info" },
{ detail: "This is a message", severity: "error" },
{ detail: "This is a message", severity: "success" },
];
}
}
- app.module.ts:
import { NgModule } from '@angular/core';
import { BrowserModule }
from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { BrowserAnimationsModule }
from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { MessagesModule } from 'primeng/messages';
import { ButtonModule } from 'primeng/button';
@NgModule({
imports: [
BrowserModule,
BrowserAnimationsModule,
MessagesModule,
ButtonModule,
FormsModule
],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule { }
输出:
例子2:这是另一个基本的例子,展示了如何使用Angular PrimeNG消息属性使用动态消息。
- app.component.html:
<div style="text-align: center">
<h2 style="color: green">GeeksforGeeks</h2>
<h5>Angular PrimeNG Messages Properties</h5>
<button type="button" pButton pRipple
(click)="showMssgs()"
label="Show Messages"
class="p-mr-3 p-button-success">
</button>
<button type="button" pButton pRipple
(click)="clearMssgs()"
label="Clear Messages"
class="p-button-danger">
</button>
<p-messages [(value)]="gfg"
[enableService]="false">
</p-messages>
</div>
- app.component.ts:
import { Component } from "@angular/core";
import { Message } from "primeng/api";
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
})
export class AppComponent {
gfg: Message[];
showMssgs() {
this.gfg = [
{
severity: "success",
summary: "Geek-Success",
detail: "This is a success Geek",
},
{
severity: "info",
summary: "Geek-Info",
detail: "This is a info Geek",
},
];
}
clearMssgs() {
this.gfg = [];
}
}
- app.module.ts:
import { NgModule } from '@angular/core';
import { BrowserModule }
from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { BrowserAnimationsModule }
from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { MessagesModule } from 'primeng/messages';
import { ButtonModule } from 'primeng/button';
@NgModule({
imports: [
BrowserModule,
BrowserAnimationsModule,
MessagesModule,
ButtonModule,
FormsModule
],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule { }
输出: