Angular PrimeNG TabView标题模板

Angular PrimeNG TabView标题模板

Angular PrimeNG是一个开源框架,拥有丰富的原生Angular UI组件,可以用来做很好的造型,这个框架用来制作响应式网站非常方便。本文将向我们展示如何在Angular PrimeNG中使用TabView Header Template。

Angular PrimeNG TabView Header Template用于设置一个标签的标题。它也支持模板化,以放置自定义的HTML内容而不是字符串。

语法:

<p-tabPanel>
    <ng-template pTemplate="header">
        Content
    </ng-template>
</p-tabPanel>

创建Angular应用程序和模块安装。

第1步:使用以下命令创建一个Angular应用程序。

ng new appname

第2步:创建你的项目文件夹即appname后,使用以下命令移动到它。

cd appname

第3步在你给定的目录中安装PrimeNG。

npm install primeng --save
npm install primeicons --save

项目结构:它将看起来像如下。

Angular PrimeNG TabView标题模板

例子1:下面是一个简单的例子,演示Angular PrimeNG TabView标题模板的使用。

  • app.component.html
<h2 style="color: green">
    GeeksforGeeks
</h2>
<h4>
    Angular PrimeNG Tabview Header Template
</h4>
<p-tabView [(activeIndex)]="index">
    <p-tabPanel>
        <ng-template pTemplate="header">
            Coding
        </ng-template>
        <p>
            Code Here
        </p>
    </p-tabPanel>
    <p-tabPanel>
        <ng-template pTemplate="header">
            Web Technologies
        </ng-template>
        <p>
            Learn About Web Technologies
        </p>
    </p-tabPanel>
    <p-tabPanel>
        <ng-template pTemplate="header">
            Articles
        </ng-template>
        <p>
            Read some Tech Articles
        </p>
    </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() { }
}
  • 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 { }

输出:

Angular PrimeNG TabView标题模板

例子2:下面是另一个例子,演示Angular PrimeNG TabView标题模板的使用。在这个例子中,我们使用的是带有标题模板的编程式标签。

  • app.component.html
<h2 style="color: green">
    GeeksforGeeks
</h2>
<h4>
    Angular PrimeNG Tabview Header Template
</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>
        <ng-template pTemplate="header">
            Coding
        </ng-template>
        <p>
            Code Here
        </p>
    </p-tabPanel>
    <p-tabPanel>
        <ng-template pTemplate="header">
            Web Technologies
        </ng-template>
        <p>
            Learn About Web Technologies
        </p>
    </p-tabPanel>
    <p-tabPanel>
        <ng-template pTemplate="header">
            Articles
        </ng-template>
        <p>
            Read some Tech Articles
        </p>
    </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() { }
}
  • 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 { }

输出:

Angular PrimeNG TabView标题模板

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程