AngularJs - 将一个句子分成由' \ n'分隔的单词。字符

时间:2016-03-28 09:22:07

标签: angularjs

<script >
    var app=angular.module('myapp',[]);
    app.controller('ctrl',function($scope){
         var spliting=function(){
         $scope.arr = [];
         $scope.arr = $scope.textvalue.split(',');
         var len = $scope.arr.length;
        var spliting=function(){

            for(var i=0;i<len;i++){
                console.log(arr[i]);
            }
        }
    }

    });

</script>

输入必须来自文本框。该函数必须拆分由换行符分隔的单词,并且每个单词必须显示在单独的文本框中。谢谢。

</head>
<body ng-controller="ctrl">
    <input type="text" ng-model="textvalue" ng-change="spliting()"> 
 </div>

1 个答案:

答案 0 :(得分:-1)

 var app = angular.module('app', []);
        app.controller('ctrl', function ($scope) {
          //  $scope.cols;
            $scope.arr = [];
             $scope.arr2 = [];
              $scope.arr3 = [];

            $scope.makeArray = function () {

                $scope.arr=$scope.cols.split(' ');
                console.log($scope.arr);


                var parent = document.getElementById("div1");
                var child = document.getElementById("inp");
                parent.removeChild(child);

            }
            $scope.makeArray2 = function () {

                $scope.arr2=$scope.cols2.split(' ');
                console.log($scope.arr2);


                var parent = document.getElementById("div2");
                var child = document.getElementById("inp2");
                parent.removeChild(child);

            }

            $scope.makeArray3 = function () {

                $scope.arr3=$scope.cols3.split(' ');
                console.log($scope.arr3);


                var parent = document.getElementById("div3");
                var child = document.getElementById("inp3");
                parent.removeChild(child);

            }

        });

HTML

<div ng-controller="ctrl" id="container">

            <div id="div1" style="text-align:left;">
                <input ng-model="cols" id="inp" type="text" ng-change="makeArray()" />
                    <div ng-repeat="x in arr track by $index" style="text-align:left;">
                        <input type="text" value={{x}}>
                    </div>
           </div>

            <div id="div2" style="text-align:center">
                <input ng-model="cols2" id="inp2" type="text" ng-change="makeArray2()" />
                    <div ng-repeat="x in arr2 track by $index" style="text-align:center">
                        <input type="text" value={{x}}>
                    </div>
           </div>

           <div id="div3" >
                <input ng-model="cols3" id="inp3" type="text" ng-change="makeArray3()" />
                    <div ng-repeat="x in arr3 track by $index" style="text-align:right">
                        <input type="text" value={{x}}>
                    </div>
           </div>

        </div>