Transcordia LLC
Presentation Example Project
https://github.com/Transcordia/ng-meetup
The source for this presentation is in the "ng-forms" directory
<form role="form" name="myForm" novalidate="novalidate">
<input type="text" ng-model="user.firstLastName" name="firstLastName"
placeholder="Enter Name">
</form>
<!--
The form directive is created. It is bound to the directive controller FormController
which is injected into its link function. The FormController initializes validation
and pristine state. Finally, myForm--a reference to the FormController--is added
to the scope.
-->
<form role="form" name="myForm" novalidate="novalidate">
<!--
Two directives are created: input directive and ngModel directive. The ngModel
directive requires as an optional controller, the FormController created above,
as well as an NgModelController, both of which are injected into its
link function as an array. The ngModelController is added to the myForm controller
as an attribute named "firstLastName"
-->
<input type="text" ng-model="user.firstLastName" name="firstLastName"
placeholder="Enter Name">
</form>
<!-- This example shall bind true or false to user.agreement -->
Agree to terms: <input type="checkbox" ng-model="user.agreement">
<!-- This example shall bind "yes" or "no" to user.agreement -->
Agree to terms: <input type="checkbox" ng-model="user.agreement"
ng-true-value="yes" ng-false-value="no" >
<input type="radio" ng-model="user.favColor" value="red"> Red <br/>
<input type="radio" ng-model="user.favColor" value="green"> Green <br/>
<input type="radio" ng-model="user.favColor" value="blue"> Blue <br/>
<!--
Using Select with hard-coded strings binds the hardcoded string to the model.
Note the use of the ngSelected directive to apply the "selected" attribute
through the expression.
-->
<select name="sex" ng-model="user.sex">
<option value="m" ng-selected="user.sex=='m'">Male</option>
<option value="f" ng-selected="user.sex=='f'">Female</option>
</select>
// controller initialized values
$scope.user.favFood = {name: 'Hamburger', group: 'Meats'};
$scope.formData = {
foods: [
{name: 'Spaghetti', group: 'Grains'},
{name: 'Cereal', group: 'Grains'},
{name: 'Steak', group: 'Meats'},
{name: 'Hamburger', group: 'Meats'},
{name: 'Apple', group: 'Fruits'},
{name: 'Lemon', group: 'Fruits'},
{name: 'Tomato', group: 'Vegetables'},
{name: 'Cucumber', group: 'Vegetables'}
]
};
// $scope.user.favFood does not === $scope.formData.foods[0]
<select ng-model="user.favFood" id="favFood" name="favFood"
ng-options="food.name for food in formData.foods track by food.id">
<option value="" selected disabled>Please select a food</option>
</select>
Built-in Validation Directives
required - Value is required
pattern - Uses a regular expression to validate the value
minlength - Minimum number of characters
maxlength - Maximum number of characters
min - Minimum numeric value
<input type="number" ng-model="user.age" name="age" min="1" max="120" required>
These states are applied to both FormController and ngModelController objects
$pristine - {boolean} - True if user has not interacted with the control yet.
$dirty - {boolean} - True if user has already interacted with the control.
$valid - {boolean} - True if there is no error.
// The ngModelController.$error object holds a map each validation on the HTML Element
userForm.age.$error = {
required: false,
min: false,
max: true
}
// The FormController.$error object holds an map of validations, each holding
// the array of ngModelController objects
userForm.$error = {
max: [
// array containing each ngController Object that violates the "max" validation
],
required: [
// array each ngController Object that violates the "required" validation
]
}
Styles are empty by default
ng-valid - is set if the model is valid.
ng-invalid - is set if the model is invalid.
ng-pristine - is set if the model is pristine.
Styles can be combined to give the meaning you desire
$parsers is an array of functions that are called when the view changes
Called during $setViewValue method
Each function is called in order passing the output as input to the subsequent function
The parser function should test for validity and call ngModel.$setValidity
Directive Controllers are used to expose an API to one or more directives in a hierarchy
Directive Controllers can have $element--the element of the directive--injected into it.