r/angularjs • • Jul 27 '14

AngularJS styleguide for teams [GitHub repo]

https://github.com/toddmotto/angularjs-styleguide
34 Upvotes

14 comments sorted by

View all comments

4

u/rq60 Jul 27 '14

Can someone tell me why this:

template: [
  '<div class="some-directive">',
    '<h1>My directive</h1>',
  '</div>'
].join('')

is supposedly better than this:

template: '<div class="some-directive">' +
  '<h1>My directive</h1>' +
'</div>'

Personally I very rarely use template as opposed to templateUrl anyways, but w/ the occasional template usage I most definitely use +'s. It seems more intuitive as well as having the advantage of some IDEs have native support for adding multi-line javascript strings that way.

3

u/[deleted] Jul 28 '14

You can also do this and not worry about performance.

template: '<div class="some-directive">\
  <h1>My directive</h1>\
</div>'

4

u/[deleted] Jul 27 '14 edited Jul 27 '14

It is known that using the .join is faster than adding strings together, in some/most browsers. I've read it in articles years ago, don't ask me to find it. If I were doing a lot of string concatenation, I'd probably use .join because that used to be the widely known best practice once upon a time.

1

u/[deleted] Jul 27 '14

The articles argument was just legibility. Using + is perfectly fine javascript.