ChipsChips is used to enter multiple values on an inputfield.

Chips - 图1

Documentation

Import

  1. import {ChipsModule} from 'primeng/chips';
  2.  

Getting Started

Chips requires an array as its model.

  1. <p-chips [(ngModel)]="values"></p-chips>
  2.  
  1. export class MyModel {
  2. values: string[];
  3. }
  4.  

Model Driven Forms

Chips can be used in a model driven form as well.

  1. <p-chips formControlName="cities"></p-chips>
  2.  

Custom Content

A chip is customized using a ng-template element where the value is passed as the implicit variable.

  1. <p-chips [(ngModel)]="values">
  2. <ng-template let-item pTemplate="item">
  3. {{item}} - (active) <i class="fa fa-user"></i>
  4. </ng-template>
  5. </p-chips>
  6.  

Properties

NameTypeDefaultDescription
fieldstringnullName of the property to display on a chip.
maxnumbernullMaximum number of entries allowed.
disabledbooleanfalseWhen present, it specifies that the element should be disabled.
stylestringnullInline style of the element.
styleClassstringnullStyle class of the element.
placeholderstringnullAdvisory information to display on input.
tabindexnumbernullIndex of the element in tabbing order.
inputIdstringnullIdentifier of the focus input to match a label defined for the component.
allowDuplicatebooleantrueWhether to allow duplicate values or not.
inputStylestringnullInline style of the input field.
inputStyleClassstringnullStyle class of the input field.
addOnTabbooleanfalseWhether to add an item on tab key press.
addOnBlurbooleanfalseWhether to add an item when the input loses focus.

Events

NameParametersDescription
onAddoriginalEvent: Browser event value: Added item value Callback to invoke when a value is added.
onRemoveoriginalEvent: Browser event value: Added item value Callback to invoke when a value is removed.
onChipClickoriginalEvent: Browser event value: Clicked item value Callback to invoke when a chip is clicked.
onFocusoriginalEvent: Browser eventCallback to invoke when a input is focused.
onBluroriginalEvent: Browser eventCallback to invoke when a input loses focus.

Styling

Following is the list of structural style classes, for theming classes visit theming page.

NameElement
ui-chipsContainer element.
ui-chips-tokenChip element container.
ui-chips-token-iconIcon of a chip.
ui-chips-token-labelLabel of a chip.
ui-chips-input-tokenContainer of input element.

Dependencies

None.

Source

View on GitHub

  1. <h3 class="first">Basic</h3>
  2. <p-chips [(ngModel)]="values1" ></p-chips>
  3. <h3>ng-template</h3>
  4. <p-chips [(ngModel)]="values2">
  5. <ng-template let-item pTemplate="item">
  6. {{item}} - (active) <i class="fa fa-user" style="margin-right:2em"></i>
  7. </ng-template>
  8. </p-chips>
  9.  
  1. export class ChipsDemo {
  2. values1: string[];
  3. values2: string[];
  4. }
  5.