Angular PrimeNG Accordion 编程

Angular PrimeNG Accordion 编程

Angular PrimeNG是一个开源框架,拥有丰富的原生Angular UI组件,可用于出色的造型,这个框架可用于制作响应式网站,非常方便。在这篇文章中,我们将了解如何在Angular PrimeNG中使用Accordion Programmatic。我们还将学习属性,以及在代码中使用的语法。

Accordion组件是用来在标签中显示一部分自定义内容的。

启用所选属性可以启用标签的可切换行为,允许用户使用可切换按钮来切换标签。

语法:

<p-button
    [icon]="..."
    (click)="..."
    styleClass="..."
    label="...">
</p-button>

<p-accordion">
    <p-accordionTab header="..." 
     [(selected)]="...>
     ...
    </p-accordionTab>
     ...
</p-accordion>

Angular PrimeNG Accordion程序化属性:

  • header。它指定了标签的标题。它是字符串数据类型,默认值为空。
  • selected。它定义了标签是否被激活。它是布尔数据类型,默认值是false。
  • style。它是组件的内联风格。它的数据类型为字符串,默认值为空。
  • styleClass:它是组件的风格类别。它的数据类型是字符串,默认值是false。

Angular PrimeNG Accordion Programmatic Event:

  • onClose。它是一个回调,通过点击标题来触发。
  • onOpen: 它是一个回调,当标签被展开时被触发。

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

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

ng new appname

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

cd appname

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

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

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

Angular PrimeNG Accordion Programmatic

运行应用程序的步骤:运行以下命令以查看输出。

ng serve --open

例子1:下面的例子说明了Angular PrimeNG Accordion Programmatic的使用。

<h2 style="color: green">GeeksforGeeks</h2>
<h5>Angular PrimeNG Accordion Programmatic</h5>
<div class="p-py-2">
    <p-button
        [icon]="activeTab[0]?'pi pi-minus':'pi pi-plus'"
        (click)="gfg(0)"
        styleClass="p-button-text"
        label="Course 1">
    </p-button>
    <p-button
        [icon]="activeTab[1]?'pi pi-minus':'pi pi-plus'"
        (click)="gfg(1)"
        styleClass="p-button-text ml-2"
        label="Course 2">
    </p-button>
</div>
<p-accordion>
    <p-accordionTab 
        header="Course I" 
        [(selected)]="activeTab[0]">
        <img src=
"https://media.geeksforgeeks.org/img-practice/banner/complete-interview-preparation-thumbnail.png?v=19265"
          height="300"
          width="350"
          alt="gfg"
        />
    </p-accordionTab>
    <p-accordionTab 
        header="Course II" 
        [(selected)]="activeTab[1]">
        <img src=
"https://media.geeksforgeeks.org/img-practice/banner/system-design-live-thumbnail.png?v=19265"
          height="300"
          width="350"
          alt="gfg"
        />
    </p-accordionTab>
</p-accordion>
import { Component } from '@angular/core';
import { MessageService } from 'primeng/api';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    providers: [MessageService],
})
export class AppComponent {
    activeTab: boolean[] = [true, false, false];
  
    gfg(index: number) {
        this.activeTab[index] = !this.activeTab[index];
    }
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule }
    from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { ButtonModule } from 'primeng/button';
import { AccordionModule } from 'primeng/accordion';
import { ToastModule } from 'primeng/toast';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        AccordionModule,
        ButtonModule,
        ToastModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
  
export class AppModule { }

输出:

Angular PrimeNG Accordion Programmatic

例子2:下面是另一个例子,说明了**Angular PrimeNG Accordion Programmatic的使用。

<h2 style="color: green">GeeksforGeeks</h2>
<h5>Angular PrimeNG Accordion Programmatic</h5>
<div class="p-py-2">
    <p-button 
        [icon]="activeTab[0]?'pi pi-minus':'pi pi-plus'" 
        (click)="gfg(0)" styleClass="p-button-text"
        label="Framework 1">
    </p-button>
    <p-button 
        [icon]="activeTab[1]?'pi pi-minus':'pi pi-plus'" 
        (click)="gfg(1)" styleClass="p-button-text ml-2"
        label="Framework 2">
    </p-button>
</div>
  
<p-accordion>
    <p-accordionTab header="Angular PrimeNG" 
        [(selected)]="activeTab[0]">
        <p>
            Angular PrimeNG is a framework used with angular
            to create components with great styling and this
            framework is very easy to use and is used to make
            responsive websites.
        </p>
    </p-accordionTab>
      
    <p-accordionTab header="React JS" 
        [(selected)]="activeTab[1]">
        <p>
            React uses a declarative paradigm that makes it
            easier to reason about your application and aims
            to be both efficient and flexible. It designs simple
            views for each state in your application, and React
            will efficiently update and render just the right
            component when your data changes.
        </p>
    </p-accordionTab>
</p-accordion>
import { Component } from '@angular/core';
import { MessageService } from 'primeng/api';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    providers: [MessageService],
})
export class AppComponent {
    activeTab: boolean[] = [true, false, false];
  
    gfg(index: number) {
        this.activeTab[index] = !this.activeTab[index];
    }
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule }
    from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { ButtonModule } from 'primeng/button';
import { AccordionModule } from 'primeng/accordion';
import { ToastModule } from 'primeng/toast';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        AccordionModule,
        ButtonModule,
        ToastModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
  
export class AppModule { }

输出:

Angular PrimeNG Accordion Programmatic

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程