将AngularJS中每个句子的第一个字母大写

时间:2016-09-01 15:05:56

标签: angularjs

当第一句完成并且用户按下点功能时必须使其他句子的第一个字母大写但是当第二个功能开始工作时,所有内容都混合了。

在第一个字母完成后,第二个字母的数量有限取决于在点之前按下多少个字符,如果我写了长信就完全有效。

这是完整的代码。



var app = angular.module('myApp', [])
app
  .directive('skywalker', function ($timeout) {
      return {
        link: function (scope, element, attrs, modelCtrl) {
          var capitalize = function (sentence) {
            if (typeof (sentence) === 'undefined' || sentence === null ||
              sentence === '' || sentence.trim('') === '') { return sentence; }

            var newSentence = sentence.substring(0, 1).toUpperCase() + sentence.substring(1, sentence.length);

            var tempList = [];
            var sondaNoktaVar = newSentence.substring(newSentence.length-1) === '.' ? true : false;
            tempList = newSentence.split('.');
            if (tempList.length > 1) {
              var tempNewSentece = tempList[0];
              if(tempList.length === 2 && tempList[tempList.length-1] === ''){
                tempNewSentece = tempNewSentece + '.';
              }


              for (var e = 1; e < tempList.length; e++) {
                  if(tempList[e] !== ''){
                    var tempNewSenteceElma = angular.copy(tempList[e]);
                    tempNewSenteceElma = tempNewSenteceElma.trim('');
                    var elma = tempList[e].split(tempNewSenteceElma)
                    elma = elma[0];


                       
                    tempNewSenteceElma = tempNewSenteceElma.substring(0, 1).toUpperCase() + tempNewSenteceElma.substring(1, tempNewSentece.length);
                    
                    tempNewSentece = tempNewSentece + 
                                      (tempNewSentece.substring(6) === '.' ? '' : '.') + 
                                      elma + 
                                      tempNewSenteceElma;

                  }
              }
              newSentence = tempNewSentece + (sondaNoktaVar === true ? '.':'');


            }



            return newSentence;
          };

          element.on('keyup', function (ev) {
            if ( ev.shiftKey === true) {
             
            }
            else if (ev.which === 13 && ev.shiftKey === true) {
              
            }
            else if (ev.which === 13 ) {
              
            }
            else {
              scope.newCommentContent = capitalize(scope.newCommentContent);
            }

          });

        }
      };
    })

  .controller('capitalizeController', [
      '$scope' ,
      function($scope) {

        $scope.newCommentContent = '';

        $scope.CommentList = [];

        $scope.Clear = function(){
          $scope.newCommentContent = '';
          $scope.CommentList = [];
        };

        $scope.addNewComment = function (ev) {
          if (ev.which === 13 && ev.shiftKey === true) {
            return;
          }
          else if (ev.which === 13) {
            if (typeof ($scope.newCommentContent) === 'undefined' || $scope.newCommentContent === null ||
              $scope.newCommentContent === '' || $scope.newCommentContent.trim('') === '') { return; }
            ev.preventDefault();
            ev.stopPropagation();

            var newComment = {
              Id: ($scope.CommentList.length +1),
              content: $scope.newCommentContent,
            }

            $scope.CommentList.push(newComment);
            $scope.newCommentContent = '';
          }
          else if (ev.which === 27) {
            ev.target.blur();
            $scope.newCommentContent = '';
          }
        };

    }]);

      
&#13;
  
<!doctype html>
<html>

	<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
	<script src="myCtrl.js"></script>
		<head>
			<meta charset="UTF-8">
			<title>control</title>
			<style>input.ng-invalid{border:1px sloid red;}</style>
		</head>
		
		<body ng-app="myApp">

			<br>
			<div ng-controller="capitalizeController"
			style="border:1px solid red; padding:10px">

						<br>
						Test <span style="color:blue; cursor:pointer;" ng-click="Clear()">Clear</span>
						<br>
						<div ng-repeat="comment in CommentList track by comment.Id"
						style="border: 1px solid gray;padding: 5px;width: 300px;margin: 5px;">
								{{comment.content}}
						</div>
						<br/>
						<textarea skywalker
			                  ng-attr-placeholder="'Write a comment and press enter'}}"
			                  spellcheck="false"
			                  ng-model="newCommentContent"
			                  ng-keydown="addNewComment($event)"
			                  class="ca-field" style="height: 100px;width:300px"></textarea>
			</div>
		</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

排队

tempNewSenteceElma = tempNewSenteceElma.substring(0, 1).toUpperCase() + tempNewSenteceElma.substring(1, tempNewSentece.length);

您在最后一个括号中使用tempNewSentece - 子字符串,但您应该使用tempNewSenteceElma

此外,我认为这部分代码:

if(tempList.length === 2 && tempList[tempList.length-1] === ''){
    tempNewSentece = tempNewSentece + '.';
}

不需要。