r/angularjs • • Oct 03 '14

[Resource] 10 Top Mistakes Angular.js Developers Make

http://www.airpair.com/angularjs/posts/top-10-mistakes-angularjs-developers-make
74 Upvotes

17 comments sorted by

14

u/lemon-meringue Oct 03 '14

This works well until you're ready to go to production and want to minify your code. [...]

ng-annotate to the rescue! I personally hate the ['$scope', ..., function(...) {}] notation because it permits renaming the variables but that's just me.

4

u/TheAceOfHearts Oct 04 '14

I think ng-annotate is great for applications, but if you're making a library you should use the array notation, IMO.

3

u/jmcunningham Oct 03 '14

I'm a fan of ng-annotate, too. Especially the use of @ngInject in the comments before a function definition

12

u/sixsence Oct 03 '14

Great read, good points.

8 Scoping $scope's

I think "controller as" syntax should definitely be mentioned here as a better alternative to using $scope prototype inheritance to share data. This will prevent having to worry about the nuances of prototype inheritance, and follows the best practices of using services to share data.

1

u/toddmotto Oct 06 '14

Agreed, was surprised controllerAs wasn't mentioned - the Object scoping inside controllers was a good solution prior to controllerAs tho! :)

4

u/[deleted] Oct 04 '14

I don't agree that many of these are mistakes. Not using a particular browser extension or not organizing files the way he likes it are not mistakes. This post should be called "10 Top things I like to do when I work with Angular".

2

u/strayWookie Oct 04 '14

Todd Motto and John Papa both have style guides out that mostly agree with each other. I'd provide links but am on mobile. Check them out, really good stuff.

2

u/rodrigo-silveira Oct 04 '14

Everything he said about factory is also true for service.

2

u/CastorNotPollux Oct 04 '14

TIL I'm more of a noob than I thought.

3

u/[deleted] Oct 04 '14

I see #2 mentioned a lot and I don't understand why it is a problem. You can still organize your code in the modular way like #1 points out. What is the real benefit of putting all your views/services/directives/controllers in their own angular modules? Is it for reusing code across applications?

4

u/joshfinnie Oct 04 '14

We currently have a large angular/django site and we take a lot of lessons from the modularity of django and it works wonders. I think people are too caught up is the way that either the tutorials or yeoman creates things for them and they don't think.

1

u/[deleted] Oct 04 '14

I'm not that familiar with django, can you expand a bit? I thought it took a rails-like approach and had separate folders for views/controllers etc. Are you following something similar for angular or are you doing small ui modules like the article suggested?

My original question was more about the folks who do something like have one top level app and then inject all these minimodules like this:

angular.module('myApp', ['myApp.views', 'myApp.library', 'myApp.directives'])

Do you do it like that?

2

u/joshfinnie Oct 04 '14

Django is usually comprised of "apps" within your large web application, and each of those "apps" are self-contained modules with views, models, "controllers"... We try to mirror this in Angular and it has worked out pretty well. In our app, we have a main app.js which pulls in all of our "modules" and each module then contains the controllers, services, and models. For example:

/* app.js */
var app = angular.module('app', ['module1', module2', ...])

Then each module has an __init__.js file which will further inject the services, controllers and directives.

/* module1/__init__.js (remember python-esque) */
angular.module('module1', ['module1.services', 'module1.controllers', 'module1.directives'])

And each module has separate controllers, services, and directives as needed.

/* module1/controllers/mainCtrl.js */
angular.module('module1')
    .controller('main', ["injection1", "injection2", function (injection1, injection2) {
    /* Stuff here... */
    };

Excuse me if there are some slight errors in my above examples as we use coffeescript at work and haven't written Angular code in javascript in some time.

Hope that clears up my comment a little, and let me know if there are any other questions or comments! Thanks!

2

u/[deleted] Oct 04 '14

Yeah, I guess that's part of the beauty (lol) of angular - it's low-level and unopinionated enough that people from different paradigms and frameworks can organize it in ways that make sense to them. The rails guys do it one way, the python folks do it another, php people do it another way, and no one can figure what the haskell people are doing.

I'd like to see some data on if/how splitting an app into multiple modules affects performance. I'm guessing it might help with testing performance but I really have no clue.

2

u/chtulhuf Oct 04 '14

There is NO problems with using a single module with an Angualr application. You can hear it here from Misko (Angular's creator): https://www.youtube.com/watch?v=ZhfUv0spHCY&t=34m19s

Modules, as I see them today are for:

  • 3rd party libraries.

  • Mock overrides

Angular doesn't support any kind of namespacing, nor lazy loading, nor partial loading. These restrictions makes 99% of the use of modules that I saw, completely meaningless.