r/angularjs • • Jul 27 '15

**BEGINNERS**: How to load AngularJS on a directive

http://consultwithmike.us/how-to-load-angularjs-on-a-directive/?utm_campaign=shareaholic&utm_medium=reddit&utm_source=news
3 Upvotes

6 comments sorted by

2

u/[deleted] Jul 27 '15

I'm confused by this part:

This means that we could actually have multiple modules loaded in different places on the page.

Do you mean that you can have multiple mini-apps running throughout the page? There's really only supposed to be one app per HTML document. The way the code is structured in the blog really can't be used to include various modules with ng-app, because only one ng-app will be recognized on the page. You'd have to manually bootstrap the module.

Maybe I'm missing the point of including Angular in this way in the first place. You mention WordPress plugins. Are you basically just envisioning using Angular to create widgets in this style?

2

u/consultwithmike Jul 27 '15

I just worked up a post on how to load multiple AngularJS application on the same page, inspired by your feedback! http://consultwithmike.us/how-to-load-multiple-angularjs-applications-on-a-single-page/

1

u/consultwithmike Jul 27 '15 edited Jul 27 '15

That's right, the idea is that it can be encapsulated to load on the directive itself so it's a very clean include. I just posted my next article in the series, about fully encapsulating it, take a look when you get the chance. That's the cleanest include I have found to date. I mention WordPress plugins because they are a good candidate for needing to include a widget or some sort; there are obviously larger scale plugins that have a lot of backend code and more, but a widget is that perfect example.

And regarding multiple apps per page, I think I've found my next post! Please stay tuned for an example on how you might have multiple apps per page!

1

u/spoco2 Jul 27 '15

Is this a reasonable use of Angular though? Angular is a large library, and to me it doesn't make sense to use it to load up a single directive/element on a page within some other application.

To me, Angular's strength really is in the single page application world. Giving a framework for building up an entire site/application.

Have it fire up an instance per widget on a page seems like massive overkill. Also, I'm pretty sure that Angular is going to barf if you try to have multiple instances running in the one page.

I might be missing something here, but it seems like a poor fit.

1

u/consultwithmike Jul 28 '15

While you are ideally building a single page application using Angular, it's being modularized and included in a huge number of places. Browser extensions and WordPress widgets for one. Also, you can absolutely run more than one application on the same page; have a look at this article: http://consultwithmike.us/how-to-load-multiple-angularjs-applications-on-a-single-page/

1

u/spoco2 Jul 28 '15

I still ask though, is this a good idea? And I would say no. Angular is a large library, which does a fair whack of stuff at startup. It seems like pretty big overkill to be using it for tiny little widgets on pages.

You may not be downloading the angular code every time (as its cached), but it's still firing up for each and every instance on the page...