# ngForms

This page shows how to use Wijmo controls in Angular 2 forms.

Wijmo for Angular 2 components can be used in Angular Forms in the same way as native HTML input controls.

## Getting Started

In addition to the usual way of binding attributes to component values, Wijmo's Angular 2 components support binding via the ngModel directive. The ngModel Angular directive augments Wijmo components with its functionalities like validation, adding the control’s state to the form instance, automatic styling of the control using state CSS classes (like ng-dirty and ng-invalid) of ngModel. When an ngModel is used on a Wijmo component, it establishes a binding for the "main value" property of the component. This "main value" property varies from component to component, for example, the value property for InputNumber or InputDate components and the selectedValue property for ComboBox or ListBox components. In order to bind to a different property using the ngModel directive, you can specify the desired property name in the wjModelProperty attribute on the component element. For example, you can bind "text" property of InputNumber by setting [wjModelProperty]="'text'" or "selectedIndex" property of ComboBox by setting [wjModelProperty]="'selectedIndex'".

This example shows a form with native input [type=number] control and multiple Wijmo InputNumber and ComboBox components. Each of them is bound to component properties using the ngModel directive. Each of the controls defines the name attribute that forces ngModel to publish the control state in the form instance.

This example implements the evenNumber validator directive that reports errors for odd values. The validator is applied to all input controls including the native input control and Wijmo InputNumber controls. The native Angular required validator is also applied to these controls. There are a span elements next to each control that checks for the evenNumber and required error keys in the control’s state published in the ngModel instances and shows an error message if the key is found there.

This example also defines CSS rules for the ngModel state classes: ng-pristine (darker background), ng-valid (green border) and ng-invalid (red border). Notice that these rules automatically get applied to native input as well as to Wijmo InputNumber controls depending on their states.

Apart from the InputNumber controls, the form contains two ComboBox controls as well, which are bound using ngModel to different control properties. The first one is bound to the selectedValue property(default behavior), and the second one is bound to the selectedIndex property by using the wjModelProperty attribute on the control’s directive element.

<form #form1="ngForm" class="form" *ngIf="active"> <div class="form-group"> <label>Input[type=number]</label><br /> <input type="number" name="input1" [(ngModel)]="num" #input1="ngModel" evenNumber required /> <span style="color:red" [hidden]="!input1.errors?.evenNumber"> even number required </span> <span style="color:red" [hidden]="!input1.errors?.required"> required value </span> <br /> Form value: <b>{​{input1.value}​}</b> </div> <div class="form-group"> <label>InputNumber</label><br /> <wj-input-number name="inputNumber1" [(ngModel)]="num1" #inputNumber1="ngModel" [step]="1" [format]="'n0'" [isRequired]="false" evenNumber required> </wj-input-number> <span style="color:red" [hidden]="!inputNumber1.errors?.evenNumber"> even number required </span> <span style="color:red" [hidden]="!inputNumber1.errors?.required"> required value </span> <br /> Form value: <b>{​{inputNumber1.value}​}</b> </div> <div class="form-group"> <label>ComboBox</label><br /> <wj-combo-box name="valueCombo" [(ngModel)]="valueComboValue" #valueCombo="ngModel" [itemsSource]="data" [displayMemberPath]="'name'" [selectedValuePath]="'name'"> </wj-combo-box> <br /> Form value: <b>{​{valueCombo.value}​}</b> </div> <div class="form-group"> <label> ComboBox ([wjModelProperty]="'selectedIndex'") </label><br /> <wj-combo-box name="indexCombo" [(ngModel)]="indexComboValue" #indexCombo="ngModel" [wjModelProperty]="'selectedIndex'" [itemsSource]="data" [displayMemberPath]="'name'" [selectedValuePath]="'name'"> </wj-combo-box> <br /> Form value: <b>{​{indexCombo.value}​}</b> </div> <button (click)="makePristine()"> Make Pristine </button> </form>
@Component({ selector: 'app-cmp', templateUrl: 'src/app.html', directives: [CORE_DIRECTIVES, FORM_DIRECTIVES, EvenNumberValidator, wjInput.WjInputNumber, wjInput.WjComboBox, DynamicFormAppComponent, AppTab, AppTabPane,] }) export class AppCmp { active = true; num = 3; num1 = 11; data = [ { name: 'Apple Inc', lastPrice: 98.38 }, { name: 'Amazon.com, Inc.', lastPrice: 320.00 }, { name: 'Google Inc.', lastPrice: 585.81 }, { name: 'Yahoo Inc.', lastPrice: 35.68 }, ]; valueComboValue = 'Apple Inc'; indexComboValue = 0; reset() { this.active = false; setTimeout(() => this.active = true, 0); } }
/* ng-model state classes */ form input.ng-pristine, form .ng-pristine.wj-control { background-color: #f4f4f4; } .ng-valid { border-color: lightgreen; } .ng-invalid { border-color: red; }

#### Result (live):

Input[type=number]
even number required required value
Form value: {{input1.value}}
InputNumber
even number required required value
Form value: {{inputNumber1.value}}
ComboBox
Form value: {{valueCombo.value}}
ComboBox ([wjModelProperty]="'selectedIndex'")
Form value: {{indexCombo.value}}

## Dynamic Forms

This is a modification of the Angular 2 Dynamic Forms example that uses Wijmo input components. It demonstrates how to create dynamic forms driven by data.

The following modifications were made in the original sample:

- WjComboBox component is used instead of native select element.
- Added NumericQuestion (question-numeric.ts file) represented by the WjInputNumber component in a form.
- Shows a form control state at the right of a corresponding UI control, to allow you better understand how it changes.