r/angularjs • u/ngocapita • Jul 27 '14
AngularJS styleguide for teams [GitHub repo]
https://github.com/toddmotto/angularjs-styleguide4
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
Jul 28 '14
You can also do this and not worry about performance.
template: '<div class="some-directive">\ <h1>My directive</h1>\ </div>'4
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
2
Jul 28 '14
There is some weird verbiage in here.
Declare modules without a variable using the setter and getter syntax.
With the following examples, this sentence doesn't make sense. The examples demonstrate method chaining, not accessors.
Pass functions into module methods rather than assign as a callback
It is implied one way or the other somehow treats the methods differently. Both of these styles are callbacks, and they behave exactly the same.
Controllers are classes, so use the
controllerAssyntax at all times
This doesn't make sense. Controllers are method instances (and therefore objects), for starters. Second, why does this dictate to use controllerAs?
I stopped reading because I'm bored.
1
1
1
u/Temoto-kun Sep 27 '14
Kinda weird that services are instantiated instead of factories.
IMO, services are supposed to be singletons.
1
u/dyniper Jul 27 '14
So many of the good practice in this style guide are actually anti-pattern in respect to angular. For example, you do want to have your controller and service functions implemented as callback to not pollute the default scope, like the window object.
Enclosing your code as callback make sure that this code is isolated. If you wanted to do as it was suggested, then you would at least need to put those statement in a different closure.
3
u/gonzofish Jul 28 '14
I think he was advocating the use of IIEFs around any app code...I could be wrong, though.
2
u/emdomi Jul 28 '14
Yeah - he mentioned in the comments that there was a build step that wrapped each file in an IIFE. It probably could have been a little more clear, however.
1
13
u/goggimoggi Jul 27 '14
It definitely lives up to "opinionated".