2014-09-04 6 views
2

이 함수는 입력 된 각 단어의 첫 글자를 대문자로 입력해야합니다. 하지만 각도가 나를 던지고있다 "RangeError : 최대 호출 스택 크기를 초과했습니다". 그리고 여기 만angular.js 각 단어를 대문자로 사용하십시오.

myApp.directive('capitalizeFirst', function(uppercaseFilter, $parse) { 
    return { 
    require: 'ngModel', 
    link: function(scope, element, attrs, modelCtrl) { 
     var capitalize = function(inputValue) { 
      var capitalized = inputValue.split(' ').reduce(function(prevValue, word){ 
      return prevValue + word.substring(0, 1).toUpperCase() + word.substring(1)+' '; 
     }, ''); 
      if(capitalized !== inputValue) { 
       modelCtrl.$setViewValue(capitalized); 
       modelCtrl.$render(); 
      }   
      return capitalized; 
     } 
     var model = $parse(attrs.ngModel); 
     modelCtrl.$parsers.push(capitalize); 
     capitalize(model(scope)); 
    } 
    }; 
}); 

라인 9. 그 '공간 렸기 때문에

http://jsfiddle.net/YyYnM/205/

누군가가 나에게 이것을 설명 할 수

바이올린입니까? 나는 이것을 한 시간 동안 알아 내려고하고있다.

+2

capitalized'가'inputValue' 동일하지 않습니다 '것 같다 추가하는 것입니다. – Axarydax

답변

2

문제는 여기

var capitalize = function(inputValue) { 
      var capitalized = inputValue.split(' ').reduce(function(prevValue, word){ 
      return prevValue + word.substring(0, 1).toUpperCase() + word.substring(1)+' '; 
     }, ''); 

입니다 그것은 당신에게 여분의 공간이 기능을 실행할 때마다 추가합니다.

각도는 순환하는 방식으로 작동합니다.

변수 중 하나의 변경이 감지되면 다른주기를 실행합니다.

여기 여분의 공간 때문에 문자열이 무한 루프에서 변경되어 각도가 벗어납니다.

못생긴 솔루션은 당신이 감소 함수의 값으로 공간을 (``) 추가되기 때문에, 이후

capitalized = capitalized.substring(0, capitalized.length - 1); 
+0

그리고 스플릿을 수정하고 공백이있는 문자에 공백을 추가하면 어떻게해야합니까? 이 같은 자본은 = (inputValue) { var에 대문자로 된 = inputValue.split ('b'). 자본금 = function (prevValue, word) { return prevValue + word.substring (0, 1) .toUpperCase() + word .substring (1) + 'b'; }, ''); 이 같은 오류를 던졌습니다 – frog

+1

이 inputValue.split ('') .map (function (element) {return element.substring (0, 1) .toUpperCase() + element.substring (1)}) .join (''); –

2

코드에 어떤 문제가 있는지 잘 모르겠지만 시도해보십시오. DEMO

myApp.directive('capitalizeFirst', function (uppercaseFilter, $parse) { 
    return { 
     require: 'ngModel', 
     scope: { 
      ngModel: "=" 
     }, 
     link: function (scope, element, attrs, modelCtrl) { 

      scope.$watch("ngModel", function() { 
       scope.ngModel = scope.ngModel.replace(/^(.)|\s(.)/g, function(v){ return v.toUpperCase(); }); 
      }); 
     } 
    }; 
}); 
+0

귀하의 답변은 훌륭하지만 사용자가 모든 데이터를 지울 때 정의되지 않은 오류가 발생합니다. 이것을 피하기 위해 다음과 같이 바꾸기 함수를 래핑 할 수 있습니다. if (typeof scope.ngModel! == 'undefined') { scope.ngModel = scope.ngModel.replace (/^(.) | \ s (.)/g, function (v) {return v.toUpperCase();}); }}); } –

1
문제는 당신이 제안 라인 (9)에 ''하지

하지만 modelCtrl의 사용. modelCtrl.$setViewValuemodelCtrl을 사용하면 모두 $parsers이 다시 실행됩니다. 따라서 스택에서 벗어날 때까지 반복적으로 capitalize 함수를 호출합니다.

입력을 대문자로 바꾸려면 문자열을 $parsers 배열로 변경하는 함수를 푸시하십시오. 이 일을 할 것입니다 :

myApp.directive('capitalizeFirst', function(uppercaseFilter, $parse) { 
    return { 
    require: 'ngModel', 
    link: function(scope, element, attrs, modelCtrl) { 
     var capitalize = function(inputValue) { 
      var capitalized = inputValue.split(' ').reduce(function(prevValue, word){ 
      return prevValue + word.substring(0, 1).toUpperCase() + word.substring(1)+' '; 
     }, '');   
      return capitalized; 
     } 
     modelCtrl.$parsers.push(capitalize); 
    } 
    }; 
}); 

function MyCtrl($scope) { 
    $scope.name = ''; 
} 
+1

David Michael Gang이 맞습니다. 내 솔루션은 텍스트 상자 안의 단어와 그의 의지를 대문자로 사용하지 않습니다. – Nikolas

관련 문제