Angular PrimeNG Form Chips自定义内容组件

Angular PrimeNG Form Chips自定义内容组件

Angular PrimeNG是一个开源框架,拥有丰富的原生Angular UI组件,可用于出色的造型,该框架可用于制作响应式网站,非常方便。这篇文章将向我们展示如何使用Angular PrimeNG表格Chips自定义内容组件

Chips组件是用来在一个单一的输入元素中接受多个值的输入。在流行的电子商务网站上,它通常被用来接受过滤器的输入。**** 我们可以通过使用一个ng-template元素来定制Chips,其中的值被作为隐含变量传递。

Angular PrimeNG Form Chips自定义内容模板:

  • item。项目模板用于为Chips设置一个自定义模板。

语法:

<p-chips [(ngModel)]="values">
   <ng-template let-item pTemplate="item">
       ...
   </ng-template>
</p-chips>

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

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

ng new appname

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

cd appname

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

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

Project Structure:

Angular PrimeNG Form Chips自定义内容组件

Project Structure

示例1:在下面的代码中,我们将利用上述语法来演示Form Chips自定义内容组件的使用。

  • app.component.html:
<div style="text-align:center;">
    <h1 style="color:green;">GeeksforGeeks</h1>
    <h3>A computer science portal for geeks</h3>
    <h4>Angular PrimeNG Form 
        Chips Custom Content Component</h4>
  
    <div class="p-fluid">
        <h5>ng-template</h5>
        <p-chips [(ngModel)]="values3">
            <ng-template let-item pTemplate="item">
                {{ item }} - (active) 
                <i class="pi pi-user p-ml-2"></i>
            </ng-template>
        </p-chips>
    </div>
</div>
  • app.component.ts:
import { Component } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html'
})
export class AppComponent {
    values3?: string[];
}
  • app.module.ts:
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 { ChipsModule } from 'primeng/chips';
import { ButtonModule } from 'primeng/button';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        ChipsModule,
        ButtonModule,
        FormsModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
  
export class AppModule { }

输出:

Angular PrimeNG Form Chips自定义内容组件

实例2:在下面的代码中,我们将利用上述语法来演示表单Chips自定义内容组件的使用。

  • app.component.html:
<div style="text-align:center;">
    <h1 style="color:green;">GeeksforGeeks</h1>
    <h3>A computer science portal for geeks</h3>
    <h4>Angular PrimeNG Form 
        Chips Custom Content Component</h4>
  
    <div class="p-fluid">
        <h5>ng-template</h5>
        <p-chips [(ngModel)]="values3">
            <ng-template let-item pTemplate="item">
                {{ item }} - (Offline) 
                <i class="pi pi-user p-ml-2"></i>
            </ng-template>
        </p-chips>
    </div>
</div>
  • app.component.ts:
import { Component } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html'
})
export class AppComponent {
    values3?: string[];
}
  • app.module.ts:
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 { ChipsModule } from 'primeng/chips';
import { ButtonModule } from 'primeng/button';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        ChipsModule,
        ButtonModule,
        FormsModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
  
export class AppModule { }

输出:

Angular PrimeNG Form Chips自定义内容组件

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程