2014-11-13 4 views
1

입력 필드에 서버 측 API에 대해 유효성을 검사하는 라이센스 키에 대한 지시문이 있습니다. 이것은 정상적으로 작동하지만 내 라이센스 키가 자동 하이픈으로 대문자로 나타나기를 원합니다.AngularJS를 사용하는 입력 필드의 자동 대문자 입력

e.e. 사용자가 abcd1234qwer5678을 입력하면 ABCD-1234-QWER-5678으로 표시됩니다. (내가 먼저 자동 총액 작업을 얻기 위해 노력하고, 다음 아픈 시도의 하이픈)

나는 그렇게

$scope.$watch('licenceKey', function (newValue, oldValue) { 
    $scope.$apply(function() { 
     $scope.licenceKey = newValue.toUpperCase(); 
    }) 
}); 

같은 컨트롤러 내에서, 먼저 시계를 몇 가지를 시도 그리고 나는 또한 두 번째를 사용하여 시도 지시문을 입력에 적용했습니다.

myApp.directive('capitalize', function() { 
    return { 
    require: 'ngModel', 
    link: function(scope, element, attrs, modelCtrl) { 
     var capitalize = function(inputValue) { 
      if(inputValue == undefined) inputValue = ''; 
      var capitalized = inputValue.toUpperCase(); 
      if(capitalized !== inputValue) { 
       modelCtrl.$setViewValue(capitalized); 
       modelCtrl.$render(); 
      }   
      return capitalized; 
     } 
     modelCtrl.$parsers.push(capitalize); 
     capitalize(scope[attrs.ngModel]); // capitalize initial value 
    } 
    }; 
}); 

첫 번째 것은 아무 것도하지 않는 것 같고 두 번째는 잠시 후 기존 텍스트를 대체하는 것처럼 보입니다. 내 HTML은 너무

<input type="text" name="licenceKey" ng-model="licenceKey" 
    ng-model-options="{ debounce : { 'default' : 150 } }" licence-key-validator /> 

내가 원하는 이유는 이러한 문제를 얻고 무엇을 달성하는 가장 좋은 방법은 무엇입니까처럼?

주목해야 할 점은 범위를 검사하기 위해 Batarang을 사용하면 양식을 제출할 때까지 licenceKey가 null 인 것으로 보입니다. 입력에 입력 할 때이 값이 채워지지 않는 이유는 무엇입니까?

angular.module('licenceApp.controllers', []) 
    .controller('licenceController', ['$scope', 'licenceAPIservice', '$filter', function ($scope, licenceAPIservice, $filter) { 
     $scope.licenceKey = ""; 

     $scope.$watch('licenceKey', function (newValue, oldValue) { 
      $scope.$apply(function() { 
       $scope.licenceKey = newValue.toUpperCase(); 
      }) 
     }); 
     ... 

업데이트

나는 내가 watch를 사용할 때, 내 텍스트 밤은 내가 유효한 라이센스 키를 얻을합니다 (licenceAPIservice에 의해 검증으로)하지만 때까지 대문자로 얻을 않습니다 대문자로 통지 단지 한 때 유효한 키를 소문자로 입력하십시오. 아래 코드 : I는 대문자와 하이픈 내가 만든 필터를 사용하여 작은 함수를 작성하는 관리

angular.module('licenceApp.directives', []) 
    .directive('licenceKeyValidator', function ($http, $q, licenceAPIservice) { 
     return { 
      require: 'ngModel', 
      link: function ($scope, element, attrs, ngModel) { 
       ngModel.$asyncValidators.licenceKeyValidator = function (licenceKey) { 
        var deferred = $q.defer(); 

        licenceAPIservice.validateKey(licenceKey).then(function (data) { 
         if (data.data) { 
          deferred.resolve(); 
         } 
         else { 
          deferred.reject(); 
         } 
        }, function() { 
         deferred.reject(); 
        }); 

        return deferred.promise; 
       }; 
      } 
     } 
    }); 

답변

1

, 살펴보고 그것이 당신의 요구 사항을 충족하는지 알려주세요.

http://plnkr.co/edit/i8MEUQjtUvlthp9WwaBq?p=preview

코드 :

var app = angular.module("myApp", []); 

app.controller('myCtrl', ['$scope', '$filter', function($scope, $filter){ 

    $scope.myText = ""; 

    $scope.update = function(){ 
    $scope.myText = $filter('myFilter')($scope.myText); 
    }; 

}]); 

app.filter('myFilter', function(){ 
    return function(text){ 
    if(!text) 
     return text; 
    else{ 
     var toReturn = text; 
     toReturn = toReturn.toUpperCase().replace('', ''); 
     if(toReturn.length > 4 && toReturn[4] !== "-") 
     toReturn = toReturn.substring(0, 4) + "-" + toReturn.substring(4); 
     if(toReturn.length > 9 && toReturn[9] !== "-") 
     toReturn = toReturn.substring(0, 9) + "-" + toReturn.substring(9); 
     if(toReturn.length > 14 && toReturn[14] !== "-") 
     toReturn = toReturn.substring(0, 14) + "-" + toReturn.substring(14); 
     return toReturn; 
    } 
    }; 
}); 

HTML :

<input ng-model="myText" ng-change="update()"/> 
관련 문제