Open In App

Angular PrimeNG Form InputSwitch Basic Component

Last Updated : 06 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 how to use the Form InputSwitch Basic Component in Angular PrimeNG. We will learn about the properties, along with their syntaxes that will be used in the code.

The InputSwitch component allows a user to switch between the two values ie, selecting either of them by toggling it.

Angular PrimeNG Form InputSwitch Basic Component Properties:

  • disabled: It specifies that the element should be disabled. It is of the boolean datatype, the default value is false.
  • readonly: it specifies that the component cannot be edited. It is of the boolean datatype, the default value is false.

 

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 --open

Example 1: This is the basic example that shows how to use the Angular PrimeNG InputSwitch Basic Component.

  • app.component.html:

HTML




<h1 style="color: green">GeeksforGeeks</h1>
<h5>Angular PrimeNG Form InputSwitch Basic</h5>
<p-inputSwitch></p-inputSwitch>


  • app.component.ts:

Javascript




import { Component } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html'
})
  
export class AppComponent { }


  • 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 {InputSwitchModule} from 'primeng/inputswitch';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        InputSwitchModule,
        FormsModule
    ],
    declarations: [ AppComponent ],
    bootstrap:    [ AppComponent ]
})
  
export class AppModule { }


Output:

 

Example 2: Below is another example that shows how to use the Angular PrimeNG InputSwitch Basic Component using the disabled and readOnly properties.

  • app.component.html:

HTML




<h1 style="color: green">GeeksforGeeks</h1>
<h5>Angular PrimeNG Form InputSwitch Basic</h5>
<p-inputSwitch disabled="true"></p-inputSwitch>
<br/>
<p-inputSwitch readonly="true"></p-inputSwitch>


  • app.component.ts:

Javascript




import { Component } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html'
})
  
export class AppComponent { }


  • 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 {InputSwitchModule} from 'primeng/inputswitch';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        InputSwitchModule,
        FormsModule
    ],
    declarations: [ AppComponent ],
    bootstrap:    [ AppComponent ]
})
  
export class AppModule { }


Output

 

Reference: https://primefaces.org/primeng/inputswitch



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads