12

I'm attempting to create a textarea who's input is bound to a JS array where entries correspond to each line in the text area.

I've been attempting to use ngList with it's optional parameter to specify the delimiter. I can make it work with the default (,) by adding a comma after each line of the textarea, but I really do not want to require that.

the textarea

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

with the input:

test1
test2
test3

The generated output is ["test1\ntest2\ntest3"]

What I am looking for is: ["test1","test2",test3"]

Plnkr Demo

6
  • Do you mean["test1","test2","test3"]? Commented Apr 8, 2014 at 15:07
  • @ChrisC yes, thank you for catching that, updating now Commented Apr 8, 2014 at 15:07
  • @ZachL I'm interested to see how you handle the initial binding... (because as soon as you add something it condenses your array to one string ["test1, test2, test3","something new"] jsfiddle.net/ncapito/wKquG/21 Commented Apr 8, 2014 at 15:13
  • @Nix, I'm not entirely sure I know what you mean... could you provide a demo perhaps (or if its worth it, a new question)? Check out this demo Commented Apr 8, 2014 at 15:23
  • I gave you a sample, just initialize your list with something in it ( $scope.list = ["1","2"];. Type a key in the text area and your list will now be ["1,2", "New item"] Commented Apr 8, 2014 at 15:30

3 Answers 3

16

This was updated in Angular 1.3 and now works like this:

<textarea ng-model="list" ng-list="&#10;" ng-trim="false"></textarea>
  • add ng-trim="false" directive to the <textarea> tag
  • use the ASCII code for newline character: &#10;
Sign up to request clarification or add additional context in comments.

1 Comment

In case I want to update the ng-model and want to reflect it in the textarea, it does not work with ng-trim and ng-list attributes. When i remove it works. But i need this feature.
5

EDIT: As of Angular 1.3, this answer is obsolete.

Please refer to @OrangeSalty's answer


From the (1.2) ngList docs:

If specified in form /something/ then the value will be converted into a regular expression.

I just added the /'s and it magically worked!

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

updated plnkr demo

Comments

2

You could split the output angular gives you:

var result = ["test1\ntest2\ntest3"][0].split("\n")

// result = ["test1", "test2", "test3"]

(Although the solution you've posted is much better!)

2 Comments

Thanks Chris! This certainly would work, but of course I'll go for the more idiomatic approach. Funny how I bang my head against this for an hour, then within almost seconds of posting on SO, I get it working.
look like you miss the ngList.

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.