Articles in this section
Category / Section

How to get started easily with Syncfusion Angular 7 Rich Text Editor?

3 mins read

A quick start project that helps you to create an Angular 7 Rich Text Editor with a minimal code configuration.

Angular 7 Rich Text Editor

The following section explains the steps required to create a simple Angular 7 Rich Text Editor component.

Project prerequisites

Make sure that you have the compatible versions of TypeScript and Angular in your machine before starting to work on this project.

Introduction

The Angular 7 Rich Text Editor used in this project is created from the Syncfusion `ej2-angular-richtexteditor` package. You can simply define it as `<ejs-richtexteditor></ejs-richtexteditor>` within the template.

Dependencies

Before starting with this project, the Angular 7 Rich Text Editor requires to add the Syncfusion `ej2-angular-richtexteditor` package from npmjs, which are distributed in npm as @syncfusion scoped packages.

Creating Angular Project

To create the Angular project using the Angular CLI tool, follow the steps:

  1. Install the Angular CLI application in your machine.
    npm install @angular/cli@7.0.4
    

 

Note:

If you would like to follow and run the application in Angular6 or Angular5 or Angular4, you need to replace the CLI command version number with the corresponding angular version number.

 

npm install -g @angular-cli@<CLI VERSION>

  1. Now, create a new Angular project by using the command `ng new` and navigate to that folder.
    ng new <project name>
    cd <project name>
    
  1. Install the ej2-angular-richtexteditor package through the npm install command.
    npm install @syncfusion/ej2-angular-richtexteditor --save
    

Adding Angular 7 Rich Text Editor

You can add the Angular 7 Rich Text Editor component by using the `ejs-richtexteditor` directive, and the attributes used within this tag allows you define other Rich Text Editor functionalities. To add the Angular 7 Rich Text Editor, follow the steps:

  1. Import the RichTextEditorAllModule into the app.module.ts file from the ej2-angular-richtexteditor package.
  2. Import and inject the other required modules within the providers section of app.module.ts.

[app.module.ts]

import { BrowserModule } from '@angular/platform-browser';
import { RichTextEditorAllModule } from '@syncfusion/ej2-angular-richtexteditor';
import { NgModule } from '@angular/core';
import { FormsModule }   from '@angular/forms';
import { AppComponent } from './app.component';
 
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule, FormsModule, RichTextEditorAllModule
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

 

  1. Define the Angular Rich Text Editor code within the app.component.html file, which is mapped against the templateUrl option in the app.component.ts file.

[app.component.html]

<ejs-richtexteditor></ejs-richtexteditor>

 

  1. Refer to the CDN link of CSS reference within the index.html file.

[index.html]

<link href="https://cdn.syncfusion.com/ej2/material.css" rel="stylesheet" />

 

  1. Run the application with the ng serve command, and an empty Rich Text Editor will be displayed in browser.  Now, you can load the Rich Text Editor with data.

 

Screenshot

RTE default

Toolbar items configuration

If you are already imported `RichTextEditorAllModule` in app.module.ts then you don’t need to import all the required modules services separately in the providers section which are mentioned below.

To configure the toolbar options in the Rich Text Editor, import the required module services from the ej2-angular-richtexteditor package. Then, mention it in the providers section within the app.component.ts file.

By default, the Rich Text Editor does not display the toolbar. To configure toolbar items in, use `toolbarSettings` property.

 [app.component.ts]

import { Component } from '@angular/core';
    import { ToolbarService, LinkService, ImageService, HtmlEditorService, TableService } from '@syncfusion/ej2-angular-richtexteditor';
    @Component({
    selector: 'app-root',
    templateUrl: './app.component.html',    providers: [ToolbarService, LinkService, ImageService, HtmlEditorService, TableService]
    })
    export class AppComponent  {
        public tools: object = {
         items: [
                'Bold', 'Italic', 'Underline', 'StrikeThrough', '|',
                'FontName', 'FontSize', 'FontColor', 'BackgroundColor', '|',
                'LowerCase', 'UpperCase', '|', 'Undo', 'Redo', '|',
                'Formats', 'Alignments', '|', 'OrderedList', 'UnorderedList', '|',
                'Indent', 'Outdent', '|', 'CreateLink','CreateTable',
                'Image', '|', 'ClearFormat', 'Print', 'SourceCode', '|', 'FullScreen']
        };
    }
 

 

[app.component.html]

<ejs-richtexteditor #toolsRTE id='alltoolRTE' [toolbarSettings]='tools' > </ ejs-richtexteditor>

 

Screenshot

RTE toolbar

Setting value on Rich Text Editor

You can populate value to the Rich Text Editor by binding the string data to it, through the `value` property at component initialization. [app.component.ts]

import { Component } from '@angular/core';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  public value: string = `
    <p>The RichTextEditor triggers events based on its actions. </p>
      <p> The events can be used as an extension point to perform custom operations.</p>`                             
}

 

Now, assign this value to the `value` property of Angular Rich Text Editor within the app.component.html file.

[app.component.html]

<ejs-richtexteditor id='defaultRTE' [(value)]='value' ></ejs-richtexteditor>

 

Screenshot

Adding content for RTE

Getting the value on form submit

By using the ngModel, you can get the value from the ngform submit.

[app.component.ts]

import { Component,ViewChild } from '@angular/core';
import {NgForm} from '@angular/forms';
import { ToolbarService, LinkService, ImageService, HtmlEditorService, RichTextEditorComponent } from '@syncfusion/ej2-angular-richtexteditor';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
  })
export class AppComponent {
      @ViewChild('fromRTE')
    private rteEle: RichTextEditorComponent;
    public value: string = null;
      rteCreated(): void {
        this.rteEle.element.focus();
    }
    onSubmit(form: NgForm): void {
      alert(form.value.name);
    }
}

 

[app.component.html]

<div>
    <div id='content' class='box-form' style="margin: 0 auto; max-width:750px; padding:25px">
        <form (ngSubmit)="onSubmit(rteForm)" #rteForm="ngForm">
            <div class="form-group">
                <ejs-richtexteditor #fromRTE #name='ngModel' [(value)]='value' required name="name" [(ngModel)]="value"
                    (created)="rteCreated()"></ejs-richtexteditor>
                <div *ngIf="(name.invalid && name.touched)" class="alert alert-danger">
                    <div *ngIf="name.errors.required">
                        Value is required.
                    </div>
                </div>
            </div>
            <div id="btn-container">
                <button type="submit" ejs-button [disabled]="!rteForm.valid">Submit</button>
                <button type="reset" ejs-button style="margin-left: 20px">Reset</button>
            </div>
        </form>
    </div>
</div>
<style>
    .alert-danger {
        color: #a94442;
        background-color: #f2dede;
        border-color: #ebccd1;
    }
  #btn-container {
    margin-top: 10px;
  }
</style>
        

 

Run the application by using “ng serve” command from its root folder.

Now, you can view the Angular Rich Text Editor output as follows in browser.

ScreenshotForm validation

There are more options to explore with Angular 7 Rich Text Editor, and you can also try to play with the downloadable example link in this knowledge base article.

Downloadable example link: Angular 7 Rich Text Editor

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied