2017-05-29 76 views
-2

美好的一天。我有一些問題。 我已經有了輸入,我寫了一些信息。點後加空格

例子:

<div class="wizard wizardstep1" ng-controller='someCtrl'> 
     <p class="wizardtitle">Put you Theme</p> 
     <input id="taskTheme" required type="text" placeholder="Put you Theme" ng-model="taskThemeWizardInputValue" ng-change="checkThemeWizardInputValue()"> 
    </div> 

而且我有我的控制器。

例子:

$scope.checkThemeWizardInputValue = function() { 
        if ($scope.taskThemeWizardInputValue === undefined) { 
         $scope.taskThemeWizardInputValue = ""; 
         console.log($scope.taskThemeWizardInputValue); 
         console.log($scope.taskThemeWizardInputValue.length); 
        } else { 
         var strt = $scope.taskThemeWizardInputValue.split('.'); 
         for (var i = 0 ; i < strt.length; i++) { 
          strt[i] = strt[i].charAt(0).toUpperCase() + strt[i].substr(1); 
         } 
         $scope.taskThemeWizardInputValue = strt.join('.'); 
         console.log($scope.taskThemeWizardInputValue); 
         console.log(strt); 
        } 
       } 

我怎麼能點後添加空間?誰知道?

Here is link to jsfiddle with my example.

+0

在這行你想改變其他每個分裂串實現的呢? –

+0

當加入我的答案。 –

+0

所以你想這樣:美國廣播公司。業。比希? –

回答

0

我們通過增加空間比第一次和一個空字符串

function someCtrl($scope) { 
 
    $scope.checkThemeWizardInputValue = function() { 
 
\t \t \t \t  if ($scope.taskThemeWizardInputValue === undefined) { 
 
\t \t \t \t   $scope.taskThemeWizardInputValue = ""; 
 
\t \t \t \t   console.log($scope.taskThemeWizardInputValue); 
 
\t \t \t \t   console.log($scope.taskThemeWizardInputValue.length); 
 
\t \t \t \t  } else { 
 
\t \t \t \t   var strt = $scope.taskThemeWizardInputValue.split('.'); 
 
\t \t \t \t   for (var i = 0 ; i < strt.length; i++) { 
 
        var addSpace=''; 
 
        if(i>0 && strt[i].trim().length>0){ 
 
        addSpace=' '; 
 
        }      
 
\t \t \t \t    strt[i] = addSpace+strt[i].trim().charAt(0).toUpperCase() + strt[i].trim().substr(1); 
 
\t \t \t \t   } 
 
\t \t \t \t   $scope.taskThemeWizardInputValue = strt.join('.'); 
 
\t \t \t \t   console.log($scope.taskThemeWizardInputValue); 
 
\t \t \t \t   console.log(strt); 
 
\t \t \t \t  } 
 
\t \t \t \t } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app> 
 
    <div class="wizard wizardstep1" ng-controller='someCtrl'> 
 
     <p class="wizardtitle">Put you Theme</p> 
 
     <input id="taskTheme" required type="text" placeholder="Put you Theme" ng-model="taskThemeWizardInputValue" ng-change="checkThemeWizardInputValue()"> 
 
    </div> 
 
</div>

0

你可以簡單地通過改變strt.join('.')strt.join('. ')做到這一點。

+0

不幸的是,我不能不喜歡你的答案。因爲首先看看你如何加入工作,而不是寫作例plz。 –

1
$scope.checkThemeWizardInputValue = function() { 
    if ($scope.taskThemeWizardInputValue === undefined) { 
     $scope.taskThemeWizardInputValue = ""; 
     console.log($scope.taskThemeWizardInputValue); 
     console.log($scope.taskThemeWizardInputValue.length); 
    } else { 
     var strt = $scope.taskThemeWizardInputValue.split('.'); 

     for (var i = 0 ; i < strt.length; i++) { 
     strt[i] = strt[i].trim(); 
     if(strt[i].length > 0) { 
      strt[i] = ' '+strt[i].charAt(0).toUpperCase() + strt[i].substr(1); 
     } 
     } 
     $scope.taskThemeWizardInputValue = strt.join('.'); 
     console.log($scope.taskThemeWizardInputValue); 
     console.log(strt); 
    } 
    } 

這是工作fiddle

+0

當我在'後面按Backspace時它不工作。 ' –

+0

Yeap。點後面的退格不起作用。 –

+0

第二你可以做到這一點沒有第二個,你可以在一個。但退格問題仍然存在。 –

0

我建議創建一個directive以便在需要時您可以插件這種行爲,而不是寫你在每個控制器ng-change

在指令簡單行element.val(event.target.value.split(".").join(". "));將爲您工作,在指令controller參數的幫助下。

見例如fiddle