Angular PrimeNG TabView动态标签
Angular PrimeNG是一个开源框架,它有丰富的原生Angular UI组件,可以用来做很好的造型,这个框架用来做响应式网站,非常方便。在这篇文章中,我们将看到如何在Angular PrimeNG中使用TabView动态标签。
Angular PrimeNG为TabView组件提供了便利,它作为一个容器,以便将内容与标签分组。动态标签是用来动态渲染标签的,p-tabPanel组件可以使用ngFor指令动态生成。为此,selected属性必须被手动启用。
Angular PrimeNG TabView动态标签的属性:
[header]
。它表示标签面板的标题。它是string类型,其默认值是null。[selected]
。只有当标签处于活动状态或当前标签被选中时,它才被定义。它是boolean类型,其默认值是false。
语法:
<p-tabView>
<p-tabPanel [header]="item.header"
*ngFor="let item of items; let i = index"
[selected]="i == 0">
{{item.content}}
</p-tabPanel>
</p-tabView>
创建Angular应用程序和模块安装。
第1步:使用以下命令创建一个Angular应用程序。
ng new appname
第2步:创建你的项目文件夹即appname后,使用以下命令移动到它。
cd appname
第3步在你给定的目录中安装PrimeNG。
npm install primeng --save
npm install primeicons --save
项目结构:它将看起来像如下。
例子1:下面是一个简单的例子,演示Angular PrimeNG TabView动态标签的使用。
- app.component.html
<h2 style="color: green">GeeksforGeeks</h2>
<h4>Angular PrimeNG Tabview Dynamic Tabs</h4>
<p-tabView [(activeIndex)]="index">
<p-tabPanel [header]="item.header"
*ngFor="let item of items;
let i = index"
[selected]="i == 0">
{{ item.content }}
</p-tabPanel>
</p-tabView>
- app.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
providers: [],
})
export class AppComponent {
constructor() { }
ngOnInit() { }
items = [
{ header: 'Coding', content: 'Code Here' },
{ header: 'Web Technologies',
content: 'Learn About Web Technologies' },
{ header: 'Articles',
content: 'Read some Tech Articles' },
];
}
- app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule }
from '@angular/platform-browser';
import { RouterModule } from '@angular/router';
import { BrowserAnimationsModule }
from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { TabViewModule } from 'primeng/tabview';
import { ButtonModule } from 'primeng/button';
@NgModule({
imports: [
BrowserModule,
BrowserAnimationsModule,
TabViewModule,
ButtonModule,
RouterModule.forRoot([
{ path: '', component: AppComponent }
])
],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule { }
输出:
例子2:下面是另一个例子,演示Angular PrimeNG TabView动态标签的使用,我们在渲染动态标签的同时,还使用了程序化控制。
- app.component.html
<h2 style="color: green">GeeksforGeeks</h2>
<h4>Angular PrimeNG Tabview Dynamic Tabs</h4>
<div style="padding: .5em 0 1em 0">
<p-button (click)="index = 0"
label="Activate Coding Tab">
</p-button>
<br /><br />
<p-button (click)="index = 1"
label="Activate Web Technologies Tab">
</p-button>
<br /><br />
<p-button (click)="index = 2"
label="Activate Articles Tab">
</p-button>
</div>
<p-tabView [(activeIndex)]="index">
<p-tabPanel [header]="item.header"
*ngFor="let item of items;
let i = index"
[selected]="i == 0">
{{ item.content }}
</p-tabPanel>
</p-tabView>
- app.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
providers: [],
})
export class AppComponent {
constructor() { }
ngOnInit() { }
items = [
{ header: 'Coding', content: 'Code Here' },
{ header: 'Web Technologies',
content: 'Learn About Web Technologies' },
{ header: 'Articles',
content: 'Read some Tech Articles' },
];
}
- app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule }
from '@angular/platform-browser';
import { RouterModule } from '@angular/router';
import { BrowserAnimationsModule }
from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { TabViewModule } from 'primeng/tabview';
import { ButtonModule } from 'primeng/button';
@NgModule({
imports: [
BrowserModule,
BrowserAnimationsModule,
TabViewModule,
ButtonModule,
RouterModule.forRoot([
{ path: '', component: AppComponent }
])
],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule { }
输出: