Angular PrimeNG Form Editor 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 see Angular PrimeNG Form Editor Events.

The Form Editor is a Quill-based rich text editor component. 

Angular PrimeNG Form Editor Events:

  • onTextChange: This is used for a callback to invoke when the text of the editor is changed by the user.
  • onSelectionChange: This is used for a callback to invoke when the selected text of the editor changes.
  • onInit: This is used for a callback to invoke after the editor is initialized.

 

Syntax:

<p-editor (event)=function()></p-editor>

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
npm install quill --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: In this example, we will learn about Angular PrimeNG Form Editor Events Component onTextChange.

  • app.component.html:

HTML




<head>
    <link href=
"https://cdn.quilljs.com/1.3.6/quill.snow.css" rel="stylesheet">
</head>
  
<div style="width:80%">
    <h1 style="color:green">w3wiki</h1>
    <h2>Angular PrimeNG Form Editor Events Component</h2>
    <p-editor [(ngModel)]="gfg"
          [style]="{ height: '120px' }"
          (onTextChange)=onTextChange()>
    </p-editor>
</div>


  • app.component.ts:

Javascript




import { Component } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
  
export class AppComponent {
    gfg: string =
        `<div>w3wiki!</div>
        <div>It is a Computer Science Portal</div>`;
  
    onTextChange(){
        alert("Hi Geek!! Text is changed")
    }
}


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


Output:

 

Example 2: In this example, we will learn about Angular PrimeNG Form Editor Events Component about onInit.

  • app.component.html:

HTML




<head>
    <link href=
"https://cdn.quilljs.com/1.3.6/quill.snow.css" rel="stylesheet">
  
</head>
  
<div style="width:80%">
    <h1 style="color:green">w3wiki</h1>
    <h2>Angular PrimeNG Form Editor Events Component</h2>
    
    <p-editor [(ngModel)]="gfg"
          [style]="{ height: '120px' }"
          (onInit)=onInit()>
    </p-editor>
</div>


  • app.component.ts:

Javascript




import { Component } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
  
export class AppComponent {
    gfg: string =
        `<div>w3wiki!</div>
        <div>It is a Computer Science Portal</div>`;
  
    onInit(){
        alert("Hi Geek!! I am initialized")
    }
}


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


Output:

 

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



Contact Us