Days
Hours
Minutes
Seconds
x

Froala Editor v4.1.4 is Here LEARN MORE

Skip to content

Angular 2 JS

Angular and Froala WYSIWYG Editor

— Angular 4, 5, 6, 7 and 8 —


GitHub

For Angular 15+

A newer SDK for Angular 15+ is here.

Angular 1 Plugin

The Angular 1 plugin for the Froala Editor is also available.

Installation instructions

  1. Install angular-froala-wysiwyg from npm

    npm install angular-froala-wysiwyg
    
  2. Add the CSS styles on your index.html file

    <!-- index.html -->
    <link href="node_modules/froala-editor/css/froala_editor.pkgd.min.css" rel="stylesheet">
    

Update editor instructions

Run the following command to update the editor:

npm update froala-editor --save

Integration


Use with Angular CLI

Installing @angular/cli

Note: Skip this part if you already generated the application.

npm install -g @angular/cli
ng new my-app
cd my-app

Add angular-froala-wysiwyg

  1. install angular-froala-wysiwyg

    npm install angular-froala-wysiwyg --save
    
  2. open src/app/app.module.ts and add the following code:

    // Import Angular plugin.
    import { FroalaEditorModule, FroalaViewModule } from 'angular-froala-wysiwyg';
    ...
    
    @NgModule({
       ...
       imports: [FroalaEditorModule.forRoot(), FroalaViewModule.forRoot() ... ],
       ...
    })
    
  3. open angular.json file and insert a new entry into the styles array

    "styles": [
      "styles.css",
      "./node_modules/froala-editor/css/froala_editor.pkgd.min.css",
      "./node_modules/froala-editor/css/froala_style.min.css",
    ]
    
  4. in angular.json file insert a new entry into the scripts array

    "scripts": [
      "./node_modules/froala-editor/js/froala_editor.pkgd.min.js"
    ]
    
  5. open src/app/app.component.html and add

    <div [froalaEditor]>Hello, Froala!</div>
    
    

Run angular-cli

ng serve

Use with ionic v2 or v3

Create Ionic app

Note: Skip this part if you already generated the application.

npm install -g cordova ionic
ionic start myApp blank
cd myApp

Add angular-froala-wysiwyg

For v3 make sure that you use the latest version of ionic and also the latest version of angular.

To install Froala Editor in Ionic follow these steps:

  1. Run

    bash
    npm install angular-froala-wysiwyg --save
    
  2. Open src/app/app.module.ts and add:

    // Import Froala Editor.
    import "froala-editor/js/froala_editor.pkgd.min.js";
    
    // Import Angular2 plugin.
    import { FroalaEditorModule, FroalaViewModule } from 'angular-froala-wysiwyg';
    ...
    
    @NgModule({
       ...
       imports: [FroalaEditorModule.forRoot(), FroalaViewModule.forRoot() ... ],
       ...
    })
    
  3. open src/app/main.ts and add

  4. In package.json add the following:

    "config": {
        "ionic_copy": "./config/copy.config.js"
    }
    
  5. Run the following commands

    mkdir config
    cp node_modules/@ionic/app-scripts/config/copy.config.js ./config/
    
  6. Open config/copy.config.js file and add the following at the beginning of module.exports

    module.exports = {
      copyFroalaEditorCss: {
        src: ['{{ROOT}}/node_modules/froala-editor/css/froala_editor.pkgd.min.css', '{{ROOT}}/node_modules/froala-editor/css/froala_style.min.css'],
        dest: '{{BUILD}}'
      },
      ...
    }
    
  7. Open src/index.html file and add in the <head> tag:

    <link rel="stylesheet" href="build/froala_editor.pkgd.min.css">
    <link rel="stylesheet" href="build/froala_style.min.css">
    
  8. In your desired view add the Froala Editor like this:

    <div [froalaEditor]>Hello, Froala!</div>
      
    

    Run your App

    ionic serve
    

    Use with webpack

    Create webpack app

    Note: Skip this part if you already generated the application.

    git clone --depth 1 https://github.com/AngularClass/angular-starter.git
    cd angular-starter
    npm install
    

    Add angular-froala-wysiwyg

    1. install angular-froala-wysiwyg

      npm install angular-froala-wysiwyg --save
      
    2. open src/app/app.module.ts and add

      // Import the Froala Editor plugin.
      import "froala-editor/js/froala_editor.pkgd.min.js";
      
      // Import Angular plugin.
      import { FroalaEditorModule, FroalaViewModule } from 'angular-froala-wysiwyg';
      ...
      
      @NgModule({
         ...
         imports: [FroalaEditorModule.forRoot(), FroalaViewModule.forRoot(), ... ],
         ...
      })
      
    3. open src/app/app.component.ts and add to the template

      <div [froalaEditor]>Hello, Froala!</div>
      
    4. open config/webpack.common.js and add the following to CopyWebpackPlugin

      {
        from: 'node_modules/froala-editor/css/',
        to: 'assets/froala-editor/css/',
      },
      
    5. open config/head-config.common.js and add a new entry to link

      { rel: 'stylesheet', href: '/assets/froala-editor/css/froala_editor.pkgd.min.css' },
      { rel: 'stylesheet', href: '/assets/froala-editor/css/froala_style.min.css' }
      
      

    Run the webpack app

    npm run start
    

    Use with angular-seed

    Create angular-seed app

    Note: Skip this part if you already generated the application. For more details refer to: https://github.com/mgechev/angular-seed.

    git clone --depth 1 https://github.com/mgechev/angular-seed.git
    cd angular-seed
    npm install
    

    Add angular-froala-wysiwyg

    1. install angular-froala-wysiwyg

      npm install angular-froala-wysiwyg --save
      
    2. Open tools/config/project.config.ts and uncomment the following line:

      import { ExtendPackages } from './seed.config.interfaces';
      
    3. Open tools/config/project.config.ts and add:

      ...
      
      this.NPM_DEPENDENCIES = [
        ...this.NPM_DEPENDENCIES,
        { src: 'froala-editor/js/froala_editor.pkgd.min.js', inject: 'libs' },
        { src: 'froala-editor/css/froala_editor.pkgd.min.css', inject: true },
        { src: 'froala-editor/css/froala_style.min.css', inject: true }
      ];
      
      ...
      
      let additionalPackages: ExtendPackages[] = [
        // required for dev build
        {
          name:'angular-froala-wysiwyg',
          path:'node_modules/angular-froala-wysiwyg/bundles/angular-froala-wysiwyg.umd.min.js'
        },
      
        // required for prod build
        {
          name:'angular-froala-wysiwyg/*',
          path:'node_modules/angular-froala-wysiwyg/bundles/angular-froala-wysiwyg.umd.min.js'
        }
      ]
      
      this.addPackagesBundles(additionalPackages);
      
    4. Open src/client/app/home/home.module.ts and add:

      // Import Angular2 plugin.
      import { FroalaEditorModule, FroalaViewModule } from 'angular-froala-wysiwyg';
      ...
      
      @NgModule({
         ...
         imports: [FroalaEditorModule.forRoot(), FroalaViewModule.forRoot() ... ],
         ...
      })
      
    5. open src/client/app/home/home.component.html and add

      <div [froalaEditor]>Hello, Froala!</div>
        
      
    6. Run webpack app

      npm run start
      

      Use with system.js and JIT

      Create Angular app

      Note:Skip this part if you already generated the application.

      git clone https://github.com/angular/quickstart.git angular-quickstart
      cd angular-quickstart
      npm install
      

    Add angular-froala-wysiwyg

    1. install angular-froala-wysiwyg

      npm install angular-froala-wysiwyg --save
      
    2. open src/index.html and add

      <link rel="stylesheet" href="node_modules/froala-editor/css/froala_editor.pkgd.min.css">
      <link rel="stylesheet" href="node_modules/froala-editor/css/froala_style.min.css">
      
      <script src="node_modules/froala-editor/js/froala_editor.pkgd.min.js"></script>
      
    3. Open src/app/app.module.ts and add:

      // Import Angular2 plugin.
      import { FroalaEditorModule, FroalaViewModule } from 'angular-froala-wysiwyg';
      ...
      
      @NgModule({
         ...
         imports: [FroalaEditorModule.forRoot(), FroalaViewModule.forRoot(), ... ],
         ...
      })
      
    4. Open src/app/app.component.ts and add the following to the template

      <div [froalaEditor]>Hello, Froala!</div>
      
    5. Open src/systemjs.config.js file and add thefollowing to map

      map: {
        ...
        'angular-froala-wysiwyg': 'npm:angular-froala-wysiwyg/bundles/angular-froala-wysiwyg.umd.js',
        ...
      }
      
      <div [froalaEditor]>Hello, Froala!</div>
      

    6. Run app

      npm run start
      

    Use with aot

    Create Angular app

    Note: Skip this part if you already generated the application.

    git clone https://github.com/angular/quickstart.git angular-quickstart
    cd angular-quickstart
    npm install
    
    1. Install additional dependencies. Make sure that there is no UNMET PEER DEPENDENCY.

      npm install @angular/compiler-cli @angular/platform-server --save
      npm install rollup rollup-plugin-node-resolve rollup-plugin-commonjs rollup-plugin-uglify --save-dev
      npm install lite-server --save-dev
      
    2. Copy scr/tsconfig.json to a new file called tsconfig-aot.json in the root of the project, then modify it as follows.

      {
        "compilerOptions": {
          "target": "es5",
          "module": "es2015",
          "moduleResolution": "node",
          "sourceMap": true,
          "emitDecoratorMetadata": true,
          "experimentalDecorators": true,
          "lib": ["es2015", "dom"],
          "noImplicitAny": true,
          "suppressImplicitAnyIndexErrors": true,
          "typeRoots": [
            "./node_modules/@types/"
          ]
        },
      
        "files": [
          "src/app/app.module.ts",
          "src/main.ts"
        ],
      
        "angularCompilerOptions": {
         "genDir": "aot",
         "skipMetadataEmit" : true
       }
      }
      
    3. Create a configuration file (rollup-config.js) in the project root directory to tell Rollup how to process the application.

      import nodeResolve from 'rollup-plugin-node-resolve';
      import commonjs    from 'rollup-plugin-commonjs';
      import uglify      from 'rollup-plugin-uglify';
      
      export default {
        entry: 'src/main.js',
        dest: 'src/build.js', // output a single application bundle
        sourceMap: false,
        format: 'iife',
        onwarn: function(warning) {
          // Skip certain warnings
      
          // should intercept ... but doesn't in some rollup versions
          if ( warning.code === 'THIS_IS_UNDEFINED' ) { return; }
      
          // console.warn everything else
          console.warn( warning.message );
        },
        plugins: [
            nodeResolve({jsnext: true, module: true}),
            commonjs({
              include: 'node_modules/rxjs/**',
            }),
            uglify()
        ]
      };
      
    4. Update src/main.ts file for AOT:

      import { platformBrowser }    from '@angular/platform-browser';
      import { AppModuleNgFactory } from '../aot/src/app/app.module.ngfactory';
      
      console.log('Running AOT compiled');
      platformBrowser().bootstrapModuleFactory(AppModuleNgFactory);
      

    Add angular-froala-wysiwyg

    1. install angular-froala-wysiwyg

      npm install angular-froala-wysiwyg --save
      
    2. open src/index.html and add

      <link rel="stylesheet" href="node_modules/froala-editor/css/froala_editor.pkgd.min.css">
      <link rel="stylesheet" href="node_modules/froala-editor/css/froala_style.min.css">
      
      <script src="node_modules/froala-editor/js/froala_editor.pkgd.min.js"></script>
      
    3. open src/app/app.module.ts and add

      // Import Angular2 plugin.
      import { FroalaEditorModule, FroalaViewModule } from 'angular-froala-wysiwyg';
      ...
      
      @NgModule({
         ...
         imports: [FroalaEditorModule.forRoot(), FroalaViewModule.forRoot(), ... ],
         ...
      })
      
    4. open src/app/app.component.ts file and add to the template

      <div [froalaEditor]>Hello, Froala!</div>
      
    5. open rollup-config.js and add the following

      //paths are relative to the execution path
      export default {
        ...
        plugins: [
          ...
          commonjs({
            include: [
              'node_modules/rxjs/**',
              'node_modules/angular-froala-wysiwyg/**'
            ]
          }),
          ...
        ]
      }
      
      

    Run app

    node_modules/.bin/ngc -p tsconfig-aot.json
    node_modules/.bin/rollup -c rollup-config.js -c rollup-config.js
    lite-server
    

Usage


Options

You can pass editor options as Input (optional).

[froalaEditor]='options'

You can pass any existing Froala option. Consult the Froala documentation to view a list available options:

public options: Object = {
  placeholderText: 'Edit Your Content Here!',
  charCounterCount: false
}

Aditional option is used: * immediateAngularModelUpdate: (default: false). This option synchronizes the angular model as soon as a key is released in the editor. Note that it may affect performance.


Events and Methods

You can pass events with the options, with a key event and object where the key is the event name and the value is the callback function.

public options: Object = {
  placeholder: "Edit Me",
  events : {
    'froalaEditor.focus' : function(e, editor) {
      console.log(editor.selection.get());
    }
  }
}

Using the editor instance from the arguments of the callback you can call editor methods as described in the method docs.

Froala events are described in the events docs.


Model

The WYSIWYG HTML editor content model.

[(froalaModel)]="editorContent"

Pass initial content:

public editorContent: string = 'My Document\'s Title'

Use the content in other places:

<input [ngModel]="editorContent"/>
<input [(ngModel)]="editorContent"/> <!-- For two way binding -->

Other two way binding example:

<div [froalaEditor] [(froalaModel)]="editorContent"></div>
<div [froalaEditor] [(froalaModel)]="editorContent"></div>

Use it with reactive forms:

<form [formGroup]="form" (ngSubmit)="onSubmit()">
  <textarea [froalaEditor] formControlName="formModel"></textarea>
  <button type="submit">Submit</button>
</form>

If you want to use two-way binding to display the form model in other places you must include [(froalaModel)]:

<form [formGroup]="form" (ngSubmit)="onSubmit()">
  <textarea [froalaEditor] formControlName="formModel" [(froalaModel)]="form.formModel"></textarea>
  <div [froalaView]="form.formModel"></div>
  <button type="submit">Submit</button>
</form>

If you want to wrap froalaEditor directive into a component that supports reactive forms please see https://froala.component.ts from demo.


Extend functionality

You can extend the functionality by adding a custom button like bellow:

// We will make usage of the Init hook and make the implementation there.
import { Component, OnInit  } from '@angular/core';
declare var $ :any;

@Component({
  selector: 'app-demo',
  template: `<div class="sample">
               <h2>Sample 11: Add Custom Button</h2>
               <div [froalaEditor]="options" [(froalaModel)]="content" ></div>
             </div>`,


export class AppComponent implements OnInit{

  ngOnInit () {
    $.FroalaEditor.DefineIcon('alert', {NAME: 'info'});
    $.FroalaEditor.RegisterCommand('alert', {
      title: 'Hello',
      focus: false,
      undo: false,
      refreshAfterCallback: false,

      callback: () => {
        alert('Hello!', this);
      }
    });
  }

  public options: Object = {
    charCounterCount: true,
    toolbarButtons: ['bold', 'italic', 'underline', 'paragraphFormat','alert'],
    toolbarButtonsXS: ['bold', 'italic', 'underline', 'paragraphFormat','alert'],
    toolbarButtonsSM: ['bold', 'italic', 'underline', 'paragraphFormat','alert'],
    toolbarButtonsMD: ['bold', 'italic', 'underline', 'paragraphFormat','alert'],
  };
}

Special tags

You can also use the editor on img, button, input and a tags:

<img [froalaEditor] [(froalaModel)]="imgObj"/>

The model must be an object containing the attributes for your special tags. For example:

public imgObj: Object = {
  src: 'path/to/image.jpg'
};

The froalaModel will change as the attributes change during usage.

froalaModel can contain a special attribute named innerHTML which inserts innerHTML in the element: If you are using 'button' tag, you can specify the button text like this:

public buttonModel: Object = {
  innerHTML: 'Click Me'
};

As the button text is modified by the editor, the innerHTML attribute from buttonModel model will be modified too.


Specific option for special tags

angularIgnoreAttrs: (default: null) This option is an array of attributes that you want to ignore when the editor updates the froalaModel:

public inputOptions: Object = {
  angularIgnoreAttrs: ['class', 'id']
};

Manual Initialization

Gets the functionality to operate on the editor: create, destroy and get editor instance. Use it if you want to manually initialize the editor.

(froalaInit)="initialize($event)"

Where initialize is the name of a function in your component which will receive an object with different methods to control the editor initialization process.

public initialize(initControls) {
  this.initControls = initControls;
  this.deleteAll = function() {
    this.initControls.getEditor()('html.set', '');
  };
}

The object received by the function will contain the following methods:

  • initialize: Call this method to initialize the Froala Editor

  • destroy: Call this method to destroy the Froala Editor

  • getEditor: Call this method to retrieve the editor that was created. This method will return null if the editor was not yet created

Displaying HTML

To display content created with the froala editor use the froalaView directive.

[froalaView]="editorContent"

<div [froalaEditor] [(froalaModel)]="editorContent"></div>
<div [froalaView]="editorContent"></div>

Do you think we can improve this article? Let us know.

[class^="wpforms-"]
[class^="wpforms-"]
[bws_google_captcha]
<div class="gglcptch gglcptch_v2"><div id="gglcptch_recaptcha_1984295838" class="gglcptch_recaptcha"></div> <noscript> <div style="width: 302px;"> <div style="width: 302px; height: 422px; position: relative;"> <div style="width: 302px; height: 422px; position: absolute;"> <iframe src="https://www.google.com/recaptcha/api/fallback?k=6Ld6lNoUAAAAAM626LfCOrnkBFJtYZAKESFCjgv_" frameborder="0" scrolling="no" style="width: 302px; height:422px; border-style: none;"></iframe> </div> </div> <div style="border-style: none; bottom: 12px; left: 25px; margin: 0px; padding: 0px; right: 25px; background: #f9f9f9; border: 1px solid #c1c1c1; border-radius: 3px; height: 60px; width: 300px;"> <textarea id="g-recaptcha-response" name="g-recaptcha-response" class="g-recaptcha-response" style="width: 250px !important; height: 40px !important; border: 1px solid #c1c1c1 !important; margin: 10px 25px !important; padding: 0px !important; resize: none !important;"></textarea> </div> </div> </noscript></div>
[class^="wpforms-"]
[class^="wpforms-"]
[bws_google_captcha]
<div class="gglcptch gglcptch_v2"><div id="gglcptch_recaptcha_18763328" class="gglcptch_recaptcha"></div> <noscript> <div style="width: 302px;"> <div style="width: 302px; height: 422px; position: relative;"> <div style="width: 302px; height: 422px; position: absolute;"> <iframe src="https://www.google.com/recaptcha/api/fallback?k=6Ld6lNoUAAAAAM626LfCOrnkBFJtYZAKESFCjgv_" frameborder="0" scrolling="no" style="width: 302px; height:422px; border-style: none;"></iframe> </div> </div> <div style="border-style: none; bottom: 12px; left: 25px; margin: 0px; padding: 0px; right: 25px; background: #f9f9f9; border: 1px solid #c1c1c1; border-radius: 3px; height: 60px; width: 300px;"> <textarea id="g-recaptcha-response" name="g-recaptcha-response" class="g-recaptcha-response" style="width: 250px !important; height: 40px !important; border: 1px solid #c1c1c1 !important; margin: 10px 25px !important; padding: 0px !important; resize: none !important;"></textarea> </div> </div> </noscript></div>
[class^="wpforms-"]
[class^="wpforms-"]
[bws_google_captcha]
<div class="gglcptch gglcptch_v2"><div id="gglcptch_recaptcha_217265956" class="gglcptch_recaptcha"></div> <noscript> <div style="width: 302px;"> <div style="width: 302px; height: 422px; position: relative;"> <div style="width: 302px; height: 422px; position: absolute;"> <iframe src="https://www.google.com/recaptcha/api/fallback?k=6Ld6lNoUAAAAAM626LfCOrnkBFJtYZAKESFCjgv_" frameborder="0" scrolling="no" style="width: 302px; height:422px; border-style: none;"></iframe> </div> </div> <div style="border-style: none; bottom: 12px; left: 25px; margin: 0px; padding: 0px; right: 25px; background: #f9f9f9; border: 1px solid #c1c1c1; border-radius: 3px; height: 60px; width: 300px;"> <textarea id="g-recaptcha-response" name="g-recaptcha-response" class="g-recaptcha-response" style="width: 250px !important; height: 40px !important; border: 1px solid #c1c1c1 !important; margin: 10px 25px !important; padding: 0px !important; resize: none !important;"></textarea> </div> </div> </noscript></div>