about angularJS directives -


i write directive called validatechinesedirective.

define(function(){     'use strict';     return function(module){         module.directive('validatechinese',function(){             return {                 restrict:'a',                 require:'ngmodel',                 link:function(scope,ele,attr,ngmodel){                       if (!ngmodel) return;                       var maxlength = -1;                       attr.$observe('validatechinese', function(value) {                         var intval = parseint(value,10);                         maxlength = isnan(intval) ? -1 : intval;                         ngmodel.$validate();                       });                         ngmodel.$parsers.push(function(viewvalue){                             var valuearray = viewvalue.split("");                             var reg = /[\u4e00-\u9fa5\uf900-\ufa2d]/;                             var len=0;                             for(var i=0;i<valuearray.length;i++){                                 len += reg.test(valuearray[i])?4:1;                             }                             if(len<=maxlength){                                 ngmodel.$setvalidity('validatechinese',true);                             }else{                                 ngmodel.$setvalidity('validatechinese',false);                             }                             return viewvalue;                         });                 }             }         })     } }) 

html:

<input type="text" name="approveddocno" validate_chinese="4" ng-model="fundmaintenancevm.editdata.approveddocno" class="form-control"> 

the reg used matching chinese. found whether input space @ begin of input box or @ end of it. ngmodel.$parsers.push didn't trigger until type character. furthermore,when ngmodel.$parsers.push triggered, viewvalue didn't contain space @ end of input box or @ end of it.does can me ,thx.

add ng-trim="false" input element used directive.


the reason that: angular sets ng-trim true by default, trims white space in input boxes, , leads no change ngmodel.

angular.module("app", []).directive('validatechinese', function() {    return {      restrict: 'a',      require: 'ngmodel',      link: function(scope, ele, attr, ngmodel) {        if (!ngmodel) return;        var maxlength = -1;        attr.$observe('validatechinese', function(value) {          var intval = parseint(value, 10);          maxlength = isnan(intval) ? -1 : intval;          ngmodel.$validate();        });        ngmodel.$parsers.push(function(viewvalue) {          var valuearray = viewvalue.split("");          var reg = /[\u4e00-\u9fa5\uf900-\ufa2d]/;          var len = 0;          (var = 0; < valuearray.length; i++) {            len += reg.test(valuearray[i]) ? 4 : 1;          }          if (len <= maxlength) {            ngmodel.$setvalidity('validatechinese', true);          } else {            ngmodel.$setvalidity('validatechinese', false);          }          console.log('ngmodel.$parsers.push fired.');          return viewvalue;        });      }    }  })
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.4/angular.min.js"></script>  <div ng-app="app">    <input type="text" name="approveddocno" validate_chinese="4" ng-model="approveddocno" ng-trim="false" class="form-control">    {{approveddocno}}  </div>


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -