angular.widget.input

Description

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

The custom angular.inputTypes provide a shorthand for declaring new inputs. This is a sharthand for text-box based inputs, and there is no need to go through the full $formFactory widget lifecycle.

Usage

In HTML Template Binding

<input
       type="..."
       ng:model="..."
       [name="..."]
       [required]
       [ng:minlength="..."]
       [ng:maxlength="..."]
       [ng:pattern="..."]
       [ng:change="..."]></input>

Parameters

Example

 <script>
   function Ctrl() {
     this.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('{\n  \"last\":\"visitor",\n  \"name\":\"guest\"}');
    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('{\n  \"last\":\"visitor",\n  \"name\":\"\"}');
    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('{\n  \"last\":\"",\n  \"name\":\"guest\"}');
    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('{\n  \"last\":\"xx",\n  \"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 valid if longer than max length', function() {
    input('user.last').enter('some ridiculously long name');
    expect(binding('user'))
      .toEqual('{\n  \"last\":\"some ridiculously long name",\n  \"name\":\"guest\"}');
    expect(binding('myForm.lastName.$valid')).toEqual('false');
    expect(binding('myForm.lastName.$error')).toMatch(/MAXLENGTH/);
    expect(binding('myForm.$valid')).toEqual('false');
  });