# ngModel

This page shows how to bind Wijmo controls using the ngModel directive.

## Getting Started

In addition to the usual way of binding attributes to scope values, Wijmo's Angular directives support binding via the ng-model directive. The ng-model Angular directive augments Wijmo directives 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 ng-model, debouncing and so on. When an ng-model is used on a Wijmo control directive, it establishes a binding for the "main value" property of the control. This "main value" property varies from control to control, for example, the value property for InputNumber or InputDate controls and the selectedValue property for ComboBox or ListBox controls. In order to bind to a different property using the ng-model directive, you can specify the desired property name in the wj-model-property attribute on the directive element. For example, you can bind "text" property of InputNumber by setting wj-model-property="text" or "selectedIndex" property of combobox by setting wj-model-property="selectedIndex".

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

This example implements the even-number 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. There is a span element next to each control that checks for the even-number error key in the control’s state published in the form instance and shows an error message if the key is found there.

This example also defines CSS rules for the ng-model 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.

The last InputNumber control has ng-model-options="{ debounce: 1000 }", and you may observe that a bound scope property is updated with a one second delay after value in the control has changed.

Apart from the InputNumber controls, the form contains two ComboBox controls as well, which are bound using ng-model 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 wj-model-property attribute on the control’s directive element.

<form name="form1" class="form"> <div class="form-group"> <label>Input[type=number]</label><br /> <input type="number" name="input1" ng-model="num" even-number /> <span style="color:red" ng-show="form1.input1.$error.evenNumber"> even number required </span> <br /> Scope value: <b>{​{num}}</b> </div> <div class="form-group"> <label>InputNumber</label><br /> <wj-input-number name="inputNumber1" ng-model="num1" step="1" format="n0" even-number> </wj-input-number> <span style="color:red" ng-show="form1.inputNumber1.$error.evenNumber"> even number required </span> <br /> Scope value: <b>{​{num1}}</b> </div> <div class="form-group"> <label>InputNumber (debounce: 1000)</label><br /> <wj-input-number name="inputNumber2" ng-model="num2" ng-model-options="{ debounce: 1000 }" step="1" format="n0" even-number> </wj-input-number> <span style="color:red" ng-show="form1.inputNumber2.$error.evenNumber"> even number required </span> <br /> Scope value: <b>{​{num2}}</b> </div> <div class="form-group"> <label>ComboBox</label><br /> <wj-combo-box name="valueCombo" ng-model="valueComboValue" items-source="data" display-member-path="name" selected-value-path="name"> </wj-combo-box> <br /> Scope value: <b>{​{valueComboValue}}</b> </div> <div class="form-group"> <label>ComboBox (wj-model-property="selectedIndex")</label><br /> <wj-combo-box name="indexCombo" ng-model="indexComboValue" wj-model-property="selectedIndex" items-source="data" display-member-path="name" selected-value-path="name"> </wj-combo-box> <br /> Scope value: <b>{​{indexComboValue}}</b> </div> <button ng-click="form1.$setPristine()"> Make pristine </button> </form>
var app = angular.module('app'); // even-number validator directive app.directive('evenNumber', function () { return { require: 'ngModel', link: function (scope, elm, attrs, ctrl) { ctrl.$parsers.push(function (viewValue) { ctrl.$setValidity('evenNumber', !viewValue || viewValue % 2 === 0); return viewValue; }); } }; }); app.controller('appCtrl', function appCtrl($scope) { $scope.num = 2; $scope.num1 = 10; $scope.num2 = 20; $scope.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 }, ]; $scope.valueComboValue = 'Apple Inc'; $scope.indexComboValue = 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
Scope value: {{num}}
InputNumber
even number required
Scope value: {{num1}}
InputNumber (debounce: 1000)
even number required
Scope value: {{num2}}
ComboBox
Scope value: {{valueComboValue}}
ComboBox (wj-model-property="selectedIndex")
Scope value: {{indexComboValue}}