2017-02-23 47 views
0

當我動態地大寫時,我遇到了一個問題。在我的代碼中,我可以大寫任何字母,但它不是動態的,如果有一些解決方案,請告訴我。這裏是我的代碼:我怎樣才能在angularjs中動態化字母

這是我的控制器。

app.controller('myController', ['$scope', '$document',function($scope, $document) { 
    $scope.typetext = "I am Programmer"; 
    $document.on('keydown', function(e) { 
     if (e.which === 8 && e.target.nodeName !== "INPUT") { 
     e.preventDefault(); 
     } 
    }); 
    } 

這是我做資本,以一些字母:

return { 
    priority: 100, 
    require: 'ngModel', 
    restrict: 'A', 
    link: function (scope, iElement, iAttrs, controller) { 

      controller.$parsers.push(function (inputValue) { 
      var transformedInput = ''; 
      if(inputValue){ 
      for(var i=0; i<inputValue.length; i++){ 
       if(i===0 || i===5){ 
       transformedInput += inputValue.charAt(i).toUpperCase(); 
       } 
       else{ 
       transformedInput += inputValue.charAt(i).toLowerCase(); 
       } 
      } 
      } 
      if (transformedInput != inputValue) { 
      controller.$setViewValue(transformedInput); 
      controller.$render(); 
     } 

     return transformedInput; 
    }); 

和index.html

<textarea id="fieldId" class="textarea" style="resize: none" cols="30" row="2" ui-mask="{{typetext}}" ng-model="model" data-ng-trim="fasle" ng-trim="false" ng-focus="expression"> 

如果你知道解決方案幫我弄好了,或者告訴我的邏輯怎麼能如果可以的話,我會大寫。

在此先感謝。

+0

你是什麼意思它不是 「動態」?預期的和實際的結果是什麼? – Fissio

+0

以及它只適用於我是程序員。我想使它適用於來自數據庫的每個單詞。如果可能的話。我認爲它會與來自數據庫的單詞進行比較,然後進行資本化。例如,如果有些人會輸入「我們來...」,它將使W和M大寫,結果將是W coMe – brithwulf

回答

0

下面是一個指令,您可以使用該指令來大寫輸入中的選定字母。

// Code goes here 
 

 
angular.module('app', []) 
 
.controller('ctrl', function($scope) { 
 
    //$scope.value = "asdasd" 
 
}) 
 
.directive('myUppercase', function() { 
 
    return { 
 
    scope: { 
 
     positions: '=myUppercase' 
 
    }, 
 
    require: 'ngModel', 
 
    link: function(scope, elem, attrs, ngModelCtrl) { 
 
     
 
     scope.positions = scope.positions || [] 
 
     
 
     ngModelCtrl.$render = function() { 
 
     elem.val(ngModelCtrl.$viewValue || "") 
 
     } 
 
     
 
     function makeString() { 
 
     var string = elem.val() 
 
     angular.forEach(scope.positions, function(pos) { 
 
      string = string.slice(0, pos) + string.charAt(pos).toUpperCase() + string.slice(pos + 1) 
 
     }) 
 
     ngModelCtrl.$setViewValue(string) 
 
     ngModelCtrl.$render() 
 
     return string; 
 
     } 
 
     
 
     elem.bind('blur keyup change', function() { 
 
     scope.$apply(makeString); 
 
     }); 
 
    } 
 
    } 
 
})
<!DOCTYPE html> 
 
<html> 
 

 
    <head> 
 
    <script data-require="[email protected]" data-semver="1.6.0" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.0/angular.js"></script> 
 
    <link rel="stylesheet" href="style.css" /> 
 
    <script src="script.js"></script> 
 
    </head> 
 

 
    <body ng-app="app" ng-controller="ctrl"> 
 
    <input ng-model="value" my-uppercase="[0, 2]" placeholder="1st and 3rd are capitalized"> 
 
    <input ng-model="value2" my-uppercase="[0, 3, 7]" placeholder="1st, 4th and 8th are capitalized"> 
 
    </body> 
 

 
</html>

+0

這是一個很好的代碼,但這不是我所需要的。我會解釋我需要的。一些文本應該來自數據庫,它應該被定義爲我認爲的變量。例如文本是:「你」。如果用戶用小寫字母輸入y,它應該自動大寫爲Y大寫,但是你不知道哪個文本會出現。程序應該猜測它應該使用哪個字母。此代碼僅適用於識別的文本。我希望我解釋我的問題。 – brithwulf

+0

所以你的意思是你有一個應該有一定大小寫的單詞列表? – Fissio

+0

是的,它來自JSON我認爲, – brithwulf