Angular PrimeNG Form TriStateCheckbox Events
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will learn about TriStateCheckbox Events in Angular PrimeNG.
The TriCheckbox Component allows a user to make a checkbox with three states ie, true, false & null conditions.
Angular PrimeNG Form TriStateCheckbox Events:
- onChange: It is a callback that is fired on value change.
Syntax:
<p-triStateCheckbox label="..." (event)=function()> </p-triStateCheckbox>
Creating Angular application & module installation:
Step 1: Create an Angular application using the following command.
ng new appname
Step 2: After creating your project folder i.e. appname, move to it using the following command.
cd appname
Step 3: Install PrimeNG in your given directory.
npm install primeng --save npm install primeicons --save
Project Structure: It will look like the following
Steps to run the application: Run the below command to see the output
ng serve --save
Example 1: Below is the example code that illustrates the use of Angular PrimeNG TriStateCheckbox Events. In this article, we will learn about onChange function.
- app.component.html:
HTML
< h1 style = "color: green;" >w3wiki</ h1 > < h2 >Angular PrimeNG TriStateCheckbox Events</ h2 > < p-triStateCheckbox label = "TriStateCheckbox Events" (onChange)="onChange()"> </ p-triStateCheckbox > |
- app.component.ts:
Javascript
import { Component } from "@angular/core" ; @Component({ selector: "app-root" , templateUrl: "./app.component.html" }) export class AppComponent { onChange() { alert( "Hi Geek!! I am called" ); } } |
- app.module.ts:
Javascript
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 { TriStateCheckboxModule } from "primeng/tristatecheckbox" ; @NgModule({ imports: [ BrowserModule, BrowserAnimationsModule, TriStateCheckboxModule, FormsModule ], declarations: [AppComponent], bootstrap: [AppComponent] }) export class AppModule {} |
Output:
Example 2: Below is another example code that illustrates the use of Angular PrimeNG TriStateCheckbox Events. onChange events won’t work when we make TriCheckbox readonly. We will demonstrate this in the below example.
- app.component.html:
HTML
< h1 style = "color: green;" >w3wiki</ h1 > < h2 >Angular PrimeNG TriStateCheckbox Events</ h2 > < p-triStateCheckbox [readonly]="true" label = "I am read only" (onChange)="onChange()"> </ p-triStateCheckbox > |
- app.component.ts:
Javascript
import { Component } from "@angular/core" ; @Component({ selector: "app-root" , templateUrl: "./app.component.html" }) export class AppComponent { onChange() { alert( "Hi Geek!! I am called" ); } } |
- app.module.ts:
Javascript
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 { TriStateCheckboxModule } from "primeng/tristatecheckbox" ; @NgModule({ imports: [ BrowserModule, BrowserAnimationsModule, TriStateCheckboxModule, FormsModule ], declarations: [AppComponent], bootstrap: [AppComponent] }) export class AppModule {} |
Output:
Reference: http://primefaces.org/primeng/tristatecheckbox
Contact Us