Angular 2 Components for jQWidgets

Angular 2 is a development platform for building mobile and desktop web applications. It provides a way to build apps for any deployment target by reusing existing code. Using HTML as the template language, Angular 2 offers developers the possiblity to create their own components.

A. Prerequisites

  • Node.js - to verify that you have it installed, run the command node -v in cmd. It will output the current version.

Note: Make sure that you are running on the latest NodeJS and npm versions.

Important:

The guide below is based on the Angular2 Just-in-time (JiT) compilation, which is the simplest one.
For Ahead-of-time (AoT) compilation, please refer to this guide.
You can find out about the differences here: Ahead-of-time (AoT) vs Just-in-time (JiT)

B. Getting Started

Steps:

  1. Create your project folder structure.
  2. Install and configurate all required modules.
  3. Build and run the app

Step 1 - Create your root project folder

The root folder contains our index.html, a few configuration files, and the app folder which holds the main content of the application.


 /root
    /app
        /app.component.ts
        /app.module.ts
        /main.ts
    /index.html
    /package.json
    /tsconfig.json
    /systemjs.config.js
                        

Note: Structure may vary based on your application needs.

Step 2 - Install and configurate all required modules

First we need to install the Angular 2 and Typescript required modules. For that we need a package.json file. Here is ours:

After your package.json is ready type npm install in your CLI.


Then we need to configure the tsconfig.json file:


And finally the system.config.js file:

Note: Configuration may vary based on your application needs.

Note: Here we use JIT compilation which means that our 'TypeScript' files are compiled in run time. If you want your 'TypeScript' files to be transpiled to 'JavaScript' before run time please refer to Angular 2 Ahead-of-time (AoT) compiler.

Step 3 - Build and run the app

I. Create index.html

Add the needed references:

Import main.ts to index.html:

Add the <my-app></my-app> tag to the body of index.html. That's the tag where we initialize the main component.

II. Create app.module.ts

III. Create main.ts

IV. Create app.component.ts

Make a reference to jqwidgets.d.ts. It contains all the TypeScript definitions for the widgets.

Import the Angular2 key components - Component, ViewChild, AfterViewInit. Then import the needed jQWidgets components.

@Component decorator:

Now it's time to create the AppComponent class.

1) @ViewChild: It holds the reference to the widget.

2) ngAfterViewInit: All widgets have a method called createWidget that accepts either no arguments or just one - an object containing the desired settings for the widget. If there is no argument then the widget is created through attributes.
For more information about creating jQWidgets through attributes please refer to this guide.

C. Events Methods & Properties

I. Events

We need to set an additional attribute to the angularWidget tag in order to bind to the event - onClick

Note: Event Names in the Angular 2 Components are the same as the Event Names in the Javascript Widgets.
The only thing you need to do is to put "on" before the Javascript widget event name and upperCase it's first letter.

II. Methods & Properties


Every widget have a method setOptions which accepts a object as an argument. This object contains widget settings.

D. Example Files

I. index.html

II. app.component.ts

E. Initialize Widgets Through Attributes

This is another way of initializing a jQWidget.
Here we set the options in the tag itself as attributes. Lets take a look:

Properties names are put in square brackets [] and we can choose either to put the value direcly like in the width, height and colorScheme properties or get it from a property in our class - like in the max, values and tooltip properties.

Here is our class:

Events Methods & Properties

All of the these three can be done the same way as in the normal initialization, but also the properties which values we defined in our class can be changed directly:

This will altomaticly update them.

Note: Angular 2 is supported only in modern web browsers such as: Chrome, Firefox, Edge and IE11.
For IE11 you must use transpiled JavaScript files.

"Ahead Of Time" Compilation using Webpack

You can compile the app in the browser, at runtime, as the application loads, using the Just-in-time (JiT) compiler. But there is another way called "Ahead-of-time (AoT)" compilation which as said in Angular2 official site: "radically improves performance". Here we covert the components and templates to executable JavaScript files before runtime.


Steps:

  1. Create your project folder structure.
  2. Install and configurate all required modules.
  3. Build and run the app

Step 1 - Create your root project folder

The root folder contains our index.html, a few configuration files, and the app folder which holds the main content of the application.


 /root
    /app
        /app.component.ts
        /app.module.ts
        /main.ts
        /other source files
    /index.html
    /package.json
    /tsconfig.json
    /tsconfig.aot.json
    /webpack.config.js
                        

Note: Structure may vary based on your application needs.

Step 2 - Install and configurate all required modules

Apart from the package.json and tsconfig.json you will need tsconfig.aot.json and webpack.config.js files.
The tsconfig.aot.json is a normal tsconfig.json file, but with "AoT"-oriented settings.
Let's take a look at the config files:


package.json

  • ngc compiles our .ts files to .js. It is a drop-in replacement for tsc.
  • webpack gets the transpiled .js files and bundles them to a single .js file.

When we type npm start in our CLI it executes both the commands. We just need to include the bundled file in or HTML and run it.


tsconfig.json


tsconfig.aot.json

What's really new is the ngc section called angularCompilerOptions. Here we say to the compiler to skip generating metadata files with the compiled application. We have also set "rootDir" which means all of our source files must be inside the folder named "app". For more information about АОТ please refer to the official "Ahead-of-time (AoT)" compilation cookbook.


webpack.config.js

For more information about Webpack please refer to the official guide.

Step 3 - Build and run the app

I. Create index.html

Add the needed references:

Add the <my-app></my-app> tag to the body of index.html. That's the tag where we initialize the main component.
Then import the bundled file we received after running the npm start command.

II. Create app.module.ts

III. Create main.ts

IV. Create app.component.ts

Make a reference to jqwidgets.d.ts. It contains all the TypeScript definitions for the widgets.

Import the Angular2 key components - Component, ViewChild, AfterViewInit. Then import the needed jQWidgets components.

@Component decorator:

Now it's time to create the AppComponent class.

1) @ViewChild: It holds the reference to the widget.

2) ngAfterViewInit: All widgets have a method called createWidget that accepts either no arguments or just one - an object containing the desired settings for the widget. If there is no argument then the widget is created through attributes.
For more information about creating jQWidgets through attributes please refer to this guide.

Events Methods & Properties

I. Events

We need to set an additional attribute to the angularWidget tag in order to bind to the event - onDrawEnd

Note: Event Names in the Angular 2 Components are the same as the Event Names in the Javascript Widgets.
The only thing you need to do is to put "on" before the Javascript widget event name and upperCase it's first letter.

II. Methods & Properties


Every widget have a method setOptions which accepts a object as an argument. This object contains widget settings.

Example Files

I. index.html

II. app.component.ts

Two Way Data Binding

We often want to both display a data property and update that property when the user makes changes.
Let's take a look at the following example.

app.component.ts:

When we type inside the input field the country variable is automatically updated.
Let`s take a look:

Important: If you are using any of the Input based widgets like jqxInput, jqxComplexInput, jqxDateTimeInput and so on you must import the FormsModule in your app.module.ts file and add it to the @NgModel:



If you won't be using the Two Way Data Binding , remove the ngModel and all it`s needed code from the
relevant angular_xxxx.ts file:

Otherwise you will get the following error: