3

The split function works just great with space as a delimiter, but i want to split with new line as the delimiter. I've tried

$scope.arr = $scope.cols.split('\n');

But it doesn't do the trick.

JS

var app = angular.module('app', []);
app.controller('ctrl', function ($scope) {

    $scope.arr = [];

    $scope.makeArray = function () {

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

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

    }
});

HTML

<div ng-controller="ctrl">
    <div style="text-align:left;">
        <input ng-model="cols"  type="text" ng-change="makeArray()" />
        <div ng-repeat="x in arr track by $index">
            <input type="text" value={{x}}>
        </div>
    </div>           
</div>
Mr.Unknown
  • 39
  • 6

1 Answers1

1

\n is the "new line" char, while \r is the carriage return char. Most of the times (citation needed?) you will find \r\n but you can split on \r then trim() the string.

Practical experience suggests to manually check form the debug console which solution is better, so try first to split by \r and see if it's enough.

Naigel
  • 9,086
  • 16
  • 65
  • 106