Angular PrimeNG TabView事件
Angular PrimeNG是一个开源框架,它有丰富的原生Angular UI组件,可以用来做很好的造型,这个框架用来做响应式网站非常容易。在这篇文章中,我们将看到如何在Angular PrimeNG中使用TabView事件。
TabView是一个容器组件,可以用标签将内容分组。事件允许触发在应用程序中发生的不同动作。事件触发了一个回调,这个回调是与它们结合在一起的。
Angular PrimeNG TabView事件:
- onChange(event)。它是当一个标签被改变时调用的回调。
- onClose(event)。它是当一个标签被关闭时调用的回调。
语法:
<p-tabView (event-name)='...'>
</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事件的使用,我们使用的是onchange事件。
- app.component.html
<h2 style="color: green">GeeksforGeeks</h2>
<h4>Angular PrimeNG Tabview Events</h4>
<p-tabView (onChange)="handleChange($event)">
<p-tabPanel header="Coding">
<ng-template pTemplate="content">
<p>Code Here</p>
</ng-template>
</p-tabPanel>
<p-tabPanel header="Web Technologies">
<ng-template pTemplate="content">
<p>Learn About Web Technologies</p>
</ng-template>
</p-tabPanel>
<p-tabPanel header="Articles">
<ng-template pTemplate="content">
Read Some Technical Articles
</ng-template>
</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 {
handleChange(e) {
console.log('Index is :', e.index);
}
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 { }
输出:
例子2:下面是另一个演示Angular PrimeNG TabView事件使用的例子。
- app.component.html
<h2 style="color: green">GeeksforGeeks</h2>
<h4>Angular PrimeNG Tabview Events</h4>
<p-tabView (onClose)="handleChange($event)">
<p-tabPanel header="Coding" [closable]="true">
<ng-template pTemplate="content">
<p>Code Here</p>
</ng-template>
</p-tabPanel>
<p-tabPanel header="Web Technologies" [closable]="true">
<ng-template pTemplate="content">
<p>Learn About Web Technologies</p>
</ng-template>
</p-tabPanel>
<p-tabPanel header="Articles" [closable]="true">
<ng-template pTemplate="content">
Read Some Technical Articles
</ng-template>
</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 {
handleChange(e) {
console.log('Tab Number', e.index, 'is Closed');
}
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 { }
输出: