NgModelController
ng
NgModelController
provides API for the ng-model
directive. The controller contains
services for data-binding, validation, CSS updates, and value formatting and parsing. It
purposefully does not contain any logic which deals with DOM rendering or listening to
DOM events. Such DOM related logic should be provided by other directives which make use of
NgModelController
for data-binding.
This example shows how to use NgModelController
with a custom control to achieve
data-binding. Notice how different directives (contenteditable
, ng-model
, and required
)
collaborate together to achieve the desired result.
Note that contenteditable
is an HTML5 attribute, which tells the browser to let the element
contents be edited in place by the user. This will not work on older browsers.
Called when the view needs to be updated. It is expected that the user of the ng-model directive will implement this method.
Sets the control to its pristine state.
This method can be called to remove the 'ng-dirty' class and set the control to its pristine state (ng-pristine class).
Change the validity state, and notifies the form when the control changes validity. (i.e. it does not notify form if given validator is already marked as invalid).
This method should be called by validators - i.e. the parser or formatter functions.
Param | Type | Details |
---|---|---|
validationErrorKey | string | Name of the validator. the |
isValid | boolean | Whether the current state is valid (true) or invalid (false). |
Update the view value.
This method should be called when the view value changes, typically from within a DOM event handler.
For example input
and
select
directives call it.
It will update the $viewValue, then pass this value through each of the functions in $parsers
,
which includes any validators. The value that comes out of this $parsers
pipeline, be applied to
$modelValue
and the expression specified in the ng-model
attribute.
Lastly, all the registered change listeners, in the $viewChangeListeners
list, are called.
Note that calling this function does not trigger a $digest
.
Param | Type | Details |
---|---|---|
value | string | Value from the view. |
This is called when we need to determine if the value of the input is empty.
For instance, the required directive does this to work out if the input has data or not.
The default $isEmpty
function checks whether the value is undefined
, ''
, null
or NaN
.
You can override this for input directives whose concept of being empty is different to the
default. The checkboxInputType
directive does this because in its case a value of false
implies empty.
Actual string value in the view.
The value in the model, that the control is bound to.
Array of functions to execute, as a pipeline, whenever
the control reads value from the DOM. Each function is called, in turn, passing the value
through to the next. Used to sanitize / convert the value as well as validation.
For validation, the parsers should update the validity state using
$setValidity()
,
and return undefined
for invalid values.
Array of functions to execute, as a pipeline, whenever the model value changes. Each function is called, in turn, passing the value through to the next. Used to format / convert values for display in the control and validation.
function formatter(value) { if (value) { return value.toUpperCase(); } } ngModel.$formatters.push(formatter);
Array of functions to execute whenever the view value has changed. It is called with no arguments, and its return value is ignored. This can be used in place of additional $watches against the model value.
An object hash with all errors as keys.
True if user has not interacted with the control yet.
True if user has already interacted with the control.
True if there is no error.
True if at least one error on the control.