Angular PrimeNG ProgressBar组件
Angular PrimeNG是一个开源框架,它有丰富的原生Angular UI组件,可以用来做很好的造型,这个框架被用来做响应式网站,非常方便。在这篇文章中,我们将了解如何在Angular PrimeNG中使用progressBar组件。我们还将了解到属性、样式以及代码中使用的语法。
ProgressBar组件:它用于制作一个显示进程状态的条形图。
属性:
- value。它指定了进度的当前值。它接受一个数字数据类型,默认值为空。
- showValue。它指定是否显示或隐藏进度条的值。它是布尔数据类型,默认值为真。
- 单位。它指定了附加在数值上的单位符号。它是字符串数据类型,默认值是%。
- mode:它指定了进度的模式,有效值是 “确定 “和 “不确定”。它是字符串数据类型,默认值是确定的。
样式:
- p-progressbar。它是容器元素。
- p-progressbar-determinate。它是一个确定的进度条的容器元素。
- p-progressbar-indeterminate。它是一个不确定的进度条的容器元素。
- p-progressbar-value。它是一个元素,其宽度根据值而变化。
- p-progressbar-label。它是显示当前值的标签元素。
创建Angular应用程序和模块安装。
第1步:使用以下命令创建一个Angular应用程序。
ng new appname
第2步:创建你的项目文件夹即appname后,使用以下命令移动到它。
cd appname
第3步 。在你给定的目录中安装PrimeNG。
npm install primeng --save
npm install primeicons --save
项目结构 。完成安装后,它将看起来像如下。
例子1:这是一个基本的例子,说明了如何使用ProgressBar组件。
<h2>GeeksforGeeks</h2>
<h5>PrimeNG ProgressBar Component</h5>
<h5>Static Progress Bar</h5>
<p-progressBar [value]="30"></p-progressBar>
<h5>Indeterminate Progress Bar</h5>
<p-progressBar mode="indeterminate"
[style]="{'height': '6px'}">
</p-progressBar>
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html'
})
export class AppComponent {}
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 { ProgressBarModule } from 'primeng/progressbar';
@NgModule({
imports: [
BrowserModule,
BrowserAnimationsModule,
ProgressBarModule,
FormsModule
],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule {}
输出:
例2:在这个例子中,我们将制作一个动态的进度条组件,它将随机更新,并显示有无进度状态值。
<h1>GeeksforGeeks</h1>
<h4>PrimeNG ProgressBar Component</h4>
<div class="card">
<h5>Dynamic (with status value)</h5>
<p-progressBar [value]="gfg1"></p-progressBar>
<h5>Dynamic (without status value)</h5>
<p-progressBar [showValue]='false' [value]="gfg2"></p-progressBar>
</div>
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html'
})
export class AppComponent {
gfg1: number = 0;
gfg2: number = 0;
ngOnInit() {
setInterval(() => {
this.gfg1 = this.gfg1 + Math.floor(Math.random() * 10) + 1;
if (this.gfg1 >= 100) {
this.gfg1 = 100;
}
}, 1500),
setInterval(() => {
this.gfg2 = this.gfg2 + Math.floor(Math.random() * 10) + 3;
if (this.gfg2 >= 100) {
this.gfg2 = 100;
}
}, 1500);
}
}
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 { ProgressBarModule } from 'primeng/progressbar';
@NgModule({
imports: [
BrowserModule,
BrowserAnimationsModule,
ProgressBarModule,
FormsModule
],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule {}
输出: