8

Is is possible to include a newline in an Angular JS expression in an attribute tag? Something like this:

<p ng-repeat="
    foo in foos 
    | filter:{attr: 'something really long'} 
    | orderBy:bar">
  {{foo}}
</p>

EDIT: To clarify, the above doesn't work. I was wondering if there is some other syntax that allows for breaking this kind of expressions into multiple lines.

2
  • 4
    What happens if you try it? Commented Oct 25, 2013 at 12:29
  • It should work. New line (any white space) does not matter as long as you maintain syntax. Commented Oct 25, 2013 at 12:35

3 Answers 3

2

angular.js parser would be able to handle it, but there's a quick regex check before handing it to the parser (see http://docs.angularjs.org/error/ngRepeat:iexp):

Be aware, the ngRepeat directive parses the expression using a regex before sending collection and optionally id to the AngularJS parser. This error comes from the regex parsing.

I filed a bug to loosen this restriction: https://github.com/angular/angular.js/issues/5537, you can hand-patch it in the meantime, it's just 1 character: m (/regex/m).

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

5 Comments

Looks like a partial fix will be included in some 1.2.x version (milestone is set), github says 1.2.7 is due in the new year, though not sure if it will be included.
Bug opened by me is fixed, it'll in some next release.
I am currently using Angular version 1.5.8 and looks like spanning ng-repeat over multiple lines is still not working. Can anyone please confirm this?
@valafar according to github.com/angular/angular.js/commit/… the fix should be included in that version.
Ah, it works. Was a stupid mistake from my end. Thanks for the quick answer and for reporting the bug. :)
2

It is possible for an expressions to span multiple lines. But ng-repeat throws an error if you try to span the expression on multiple lines.

Take a look at this plunker: Add a new line in the ng-repeat expression and open the browser console to see the error message.

http://plnkr.co/edit/E1O8Iy3VzL3kzj72BDUL?p=preview

Comments

1

Yes, it is possible to use multiline attributes with ANY HTML element, including AngularJS directives.

1 Comment

Sure, no one will prohibit you from doing so, but the Angular code will not recognize it.

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.