angular.module.ng.$compileProvider.directive.input

Description

HTML input element control with angular data-binding. Input control follows HTML5 input types and polyfills the HTML5 validation behavior for older browsers.

Usage

as element (see IE restrictions)
<input
       ng-model="{string}"
       [name="{string}"]
       [required]
       [ng-minlength="{number}"]
       [ng-maxlength="{number}"]
       [ng-pattern="{string}"]
       [ng-change="{string}"]>
</input>

Parameters

Example

 <script>
   function Ctrl($scope) {
     $scope.user = {name: 'guest', last: 'visitor'};
   }
 </script>
 <div ng-controller="Ctrl">
   <form name="myForm">
     User name: <input type="text" name="userName" ng-model="user.name" required>
     <span class="error" ng-show="myForm.userName.$error.required">
       Required!</span><br>
     Last name: <input type="text" name="lastName" ng-model="user.last"
       ng-minlength="3" ng-maxlength="10">
     <span class="error" ng-show="myForm.lastName.$error.minlength">
       Too short!</span>
     <span class="error" ng-show="myForm.lastName.$error.maxlength">
       Too long!</span><br>
   </form>
   <hr>
   <tt>user = {{user}}</tt><br/>
   <tt>myForm.userName.$valid = {{myForm.userName.$valid}}</tt><br>
   <tt>myForm.userName.$error = {{myForm.userName.$error}}</tt><br>
   <tt>myForm.lastName.$valid = {{myForm.lastName.$valid}}</tt><br>
   <tt>myForm.userName.$error = {{myForm.lastName.$error}}</tt><br>
   <tt>myForm.$valid = {{myForm.$valid}}</tt><br>
   <tt>myForm.$error.required = {{!!myForm.$error.required}}</tt><br>
   <tt>myForm.$error.minlength = {{!!myForm.$error.minlength}}</tt><br>
   <tt>myForm.$error.maxlength = {{!!myForm.$error.maxlength}}</tt><br>
 </div>
  it('should initialize to model', function() {
    expect(binding('user')).toEqual('{"name":"guest","last":"visitor"}');
    expect(binding('myForm.userName.$valid')).toEqual('true');
    expect(binding('myForm.$valid')).toEqual('true');
  });

  it('should be invalid if empty when required', function() {
    input('user.name').enter('');
    expect(binding('user')).toEqual('{"last":"visitor"}');
    expect(binding('myForm.userName.$valid')).toEqual('false');
    expect(binding('myForm.$valid')).toEqual('false');
  });

  it('should be valid if empty when min length is set', function() {
    input('user.last').enter('');
    expect(binding('user')).toEqual('{"name":"guest","last":""}');
    expect(binding('myForm.lastName.$valid')).toEqual('true');
    expect(binding('myForm.$valid')).toEqual('true');
  });

  it('should be invalid if less than required min length', function() {
    input('user.last').enter('xx');
    expect(binding('user')).toEqual('{"name":"guest"}');
    expect(binding('myForm.lastName.$valid')).toEqual('false');
    expect(binding('myForm.lastName.$error')).toMatch(/minlength/);
    expect(binding('myForm.$valid')).toEqual('false');
  });

  it('should be invalid if longer than max length', function() {
    input('user.last').enter('some ridiculously long name');
    expect(binding('user'))
      .toEqual('{"name":"guest"}');
    expect(binding('myForm.lastName.$valid')).toEqual('false');
    expect(binding('myForm.lastName.$error')).toMatch(/maxlength/);
    expect(binding('myForm.$valid')).toEqual('false');
  });