9

I have a angularJS application, in which I have an array.

  $scope.data[0] =  x1;
  $scope.data[1] =  x2;

and a text area

  <textarea ng-model="data"> </textarea>

I can see the that textarea contains values x1, x2 (separated by comma). I want to show the values on separate lines. Meaning all array values should be separated by new line character not by comma. Do I need to write filter for this?

2
  • U can try filter and if it doesn't work, ask again :) Why didn't u try this solution? Commented Jul 11, 2013 at 6:14
  • I think it is doing toString on the array,to return x1,x2. You need to create a filter for it. Commented Jul 11, 2013 at 6:14

3 Answers 3

16

This is exactly what ng-list does:

<textarea ng-model="someArray" ng-list="/\n/"></textarea>

This also works on all other kinds of inputs as it hooks into ngModels parsers/formatters.

See this fiddle: http://jsfiddle.net/xbYzT/1/

The problem with this is that ng-list always joins with a ',' as the separator, so it's not really bi-directional.

Sign up to request clarification or add additional context in comments.

3 Comments

The only problem with this is that ngList always joins an existing array with , as the separator, ignoring the user-selected separator - even if it is not a regex.
Ted: yes, that's an ugly issue - and it's not easy to fix as you cannot really revert splitting by regexp. Updated the comment to reflect that.
AngularJS 1.3 is removing support for regexes in ngSplit, making it a moot point.
16

A much easier way to do this in Angular >= 1.3 which works both ways:

<textarea ng-model="yourStringArray" ng-list="&#10;" ng-trim="false"></textarea>

Plunker

3 Comments

This really needs more upvotes. What a great answer.
very nice brow!
@AlisomMartins, thanks, your brow looks quite nice too.
15

You can write a directive that modifies how ng-model converts variables into input values and back. I'm just writing this off the top of my head, so I have no idea if it's exactly right, but something like this might do it:

app.directive('splitArray', function() {
    return {
        restrict: 'A',
        require: 'ngModel',
        link: function(scope, element, attr, ngModel) {

            function fromUser(text) {
                return text.split("\n");
            }

            function toUser(array) {                        
                return array.join("\n");
            }

            ngModel.$parsers.push(fromUser);
            ngModel.$formatters.push(toUser);
        }
    };
});

And you can use it like this:

  <textarea ng-model="data" split-array> </textarea>

1 Comment

You can look to this answer for more examples of formatters and parsers: stackoverflow.com/questions/11616636/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.