So many missing pieces. Where to look next? [closed] - angularjs

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago.
Improve this question
Learning angular is a pretty frustrating process. I've just started the journey down this endless road a couple of weeks ago. I've gone through the official tutorial on the angular website, watched the first 40+ videos on egghead.io and read through several other tutorials and examples. I'm new to angular but have a good amount of experience with javascript, jQuery, and knockout, but even after all of the research and examples I've gone through, I still can't piece together even the simplest angular app in a real-world situation.
The problem is that most of these tutorials and examples try to show all the different parts of angular, including controllers, directives, filters, services, $http/$resource, routing, animations, etc.so you end up with this one or two controller app that doesn't have any substance but it incorporates the basics of all of these features. Now, strip away all of the features that aren't really needed for a simple app like $resource, routing, and animations, and add substance to the business logic and data management, and you quickly can see how many holes exist in these tutorials.
For example, a tutorial will show how you can share data between controllers using services. It will go over creating the controller, and using a service to get the data from a server using $http or $resource. Then it will show that even though the request for data is asynchronous, there is rarely a need for callbacks because the data-bindings will just be empty until the data arrives and then magically filled in. Now you have a reusable service to get data in all of your controllers.
Wow! everything is so simple and clean, and it just works! Angular is awesome! Then, of course, when you go to write an actual app that needs more than one controller, and the controllers all depend on the same data, and you add something called "business logic" that is also shared and uses this data, you immediately realize things aren't so simple.
First of all, as soon as you add business logic that has to use the data from the server (like with any real app), your clean code with no callbacks is broken. You now have to implement callbacks everywhere, because you have to wait for the data to arrive before you can use it. Second, prototypal scope inheritance becomes unreliable because you don't know if the data from other controllers has loaded yet or not. Worst of all though, there is nothing in these tutorials about how you are supposed to keep the data in sync between controllers, and with the server. If the data is modified in the scope of one controller, somehow all of the other controller scopes need to be updated as well...
These are just some of the complexities that are introduced even with a pretty simple app that doesn't even use routing. I'm trying to make a simple calculator that doesn't persist data, and I am stuck with all of these problems and questions and can't find any real solutions.
Can someone please point me in the right direction of a good tutorial, example, or book that will fill in these missing holes without getting into complex parts of angular that my app doesn't require?
Thanks in advance and sorry for the rant, this has been very frustrating :/

I am far from being an angularJS expert, I am actually in the process of learning too. However, we are almost done with our first angularJS MVP at work so maybe I can share some of my experience. Here are a few patterns I learnt
Your controllers should be minimalist and call services and factories.
Controller actions are triggered by $emit and $broadcast which is what keeps everything in sync. I have controllers with just a list of $scope.$on('onCustomAction',function(event,params){ $scope.myObject.doSomething(); }). Ideally, it should be a minimalist list of those.
In ng-click, you should have methods like myObject.fancyMethod() where myObject is part of the '$scope`.
Put small pieces of reusable html that have their own little controller in directives. I am quite late on that but I am starting to have a few.
Use ng-include for large pieces of html that you need to move around (ie: modal windows, main content, sidebars etc).
Remember, if it's not in the $scope, it might as well not exist.
Services can call other services and can be used for singletons and instanced objects. They should be the meat of your code.
Hope this helps. Feel free to argue each of those items.

As someone who was experiencing the same type of angularjs frustration. I get it. What helped me immensely was Jeremy Howard's 4 part tutotial, "Angularjs End to end web app in under an hour". It uses ASP.net but I am sure most of the back end examples would be easily reproducible with any other framework.
Here is a quick and dirty laundry list of why I found this useful.
Example is a Todo CRUD app that could be used as a template for most other web apps.
The videos move at a reasonable pace and are broken down into logical functional areas.
Jeremy uses some of the techniques you described above that are in other videos such as promises, directives, factories, services, link functions, controllers, etc. However, the examples he uses do have substance as they work within the context of the todo app.
Lastly, it becomes clear in watching Jeremy going through his tutorial that being very organized in how you structure your back end will make your front end angular app so much better and powerful. Think separation of concerns to a whole other level.
I could go on and on but one thing I can say, is that Jeremy's tutorials not only made me better at angularjs but made me better at being a full stack developer.
Do yourself a favor and check it out. You won't regret it. Good luck.

Here is a nice tutorial on data model dealing with your problem:
"Worst of all though, there is nothing in these tutorials about how you are supposed to keep the data in sync between controllers, and with the server. If the data is modified in the scope of one controller, somehow all of the other controller scopes need to be updated as well..."
http://www.webdeveasy.com/angularjs-data-model/

Related

Why would you use lodash in AngularJS?

Simple enough question I think. I see people raving about it but I haven't seen anything on the "why" use it. It doesn't seem to me (from my naïve outside perspective) ng-repeat, if not in that nested layer do ng-repeat inside another. I looks like that it doesn't add functionality that angular doesn't already have—I'm sure I'm wrong—
I see the term "lazy loading" being used with it and it doesn't seem like it's that much easier after seeing there docs. What are some things lodash makes significantly easier in AngularJS specifically that I would make it work adding another lib to my project? And what can you do with it that you cannot with angular out of the box?
They're just not the same, and exist for distinct reasons. I think you already know what AngularJS works for, so about your questions:
What can you do with it that you cannot with angular out of the box?
Well, if you need to deal with several data in structures like arrays, objects or mixed/nested shapes, lodash will save you a lot of time and effort.
Maybe there's a lot of items in collections which should be presented to your client application in some particular way, you would have to write a lot of JS code in Angular controllers or services with out the aid of lodash.
If there's a lot of logic tied to your data structures and/or complex algorithms and coding workflow, go for lodash.
Lodash is a great tool, you can get some intro here and of course, just check out the API reference. You can use it at everywhere, either Angular or any other framework, and of course, also at Node too!

what is the angularJS services?

I am a jquery developer, now I am learning angularJS through tutorials point, I didn't understand the concept of the angularJS service and factory. Please advise me where can I get exact details on this.
Advance thanks.
If you don't have a fundamental understanding of MVC frameworks, I would recommend first understanding the Model-View-Controller concept. Assuming you have an understanding of MVC:
Both sort of act as your 'models'. Ultimately, if you so choose, you can use either or in most cases. However, the main difference between a service and a factory is that a factory is intended to return an object, that you will interact with. Whereas a service actually gets initialized using this, and you will interact with the service using it's namespace followed by an attribute on the service.
I reccommend the article below. It really helps highlight on the difference in a way that's easy for angular newbies. I find myself referring to it from time to time, and it's currently the top result on google. Check er out,
http://tylermcginnis.com/angularjs-factory-vs-service-vs-provider/
At the end of the day, I typically find myself using services more. The use cases will vary, and like I said earlier, in most basic applications there is a good chance you could get away with using either/or.
Providers on the other hand are intended to be used quite a bit differently. Refer to the article though, as he does a much better job of explaining the nuances.

Specific suggestions on how to prepare for AngularJS 2.0?

I have an AngularJS 1.3 application that will I suppose eventually move to AngularJS 2.0. I've watched the videos from ng-europe and I understand there are many changes. As I expect development to continue with our AngularJS 1.3 code for the next 12 I would like to hear if anyone has any specific suggestions on preparing for 2.0.
My application is simple in that it creates no directives. In particular I would like to hear any tips on how I should go about creating controllers to get ready for them being very different in 2.0. Should I eliminate all references to scope in my controllers. Should I move everything to services?
I hope for some useful suggestions but of course specific ones so this question does not get closed.
Thanks
I think that so little has specifically been announced yet, and there's so much potential for it to change that it's a hard question to answer, e.g. check out this issue for an example of something quite fundamental that's likely to change since the ng-europe announcement.
Within my team we're trying to take the following approach:
Try not to use $scope (controller-as syntax).
Keep your controllers as light as possible by using directives to encapsulate UI functionality and services to deal with data models / API calls.
Use the new syntax for one-way binding where you don't need two-way binding. One-way binding will be the default for Angular 2.0.
Write some unit tests! It'll be much easier to do some significant refactoring if you have some confidence that your code will still work :).
Not only should that help with the move to 2.0 if / when that happens, but it should help your 1.3 apps too!

Less control with angularjs?

I'm still learning AngularJS, but I'm not sure if having less control (via javascript) as I read is a good thing. I mean, everything is done via bindings through HTML code / attributes. How can I access $rootScope , or any of the services other than having them injected to my controllers via 'low level' javascript code?angular global variable doesn't hold much when logged to the console. What else to use to discover possible methods accessable via javascript?
I suspect this will be closed as opinion based but here is my opinion:
Angular does not remove your ability to control the page via javascript. Instead it isolates that code that manipulates the DOM. The learning curve for angular is a little like a roller coaster, in that you feel differently about the framework after learning a new piece. I felt really great after learning how templates and controllers worked but then felt low after seeing the limitations and complexities it created only to feel really great when I learned about Directives and how to manipulate the DOM through them. Six months into it, I am comfortable with Angular and would never go back to all the boilerplate of straight JQuery/javascript.
An important thing to keep in mind. Angular is not a UI library but rather a way of organizing your development files. There are many integration libraries of well known UI frameworks into angular Directives but by far the most popular is Twitter Bootstrap.
This blog post really nailed it: https://coderwall.com/p/3qclqg

App architecture: directives vs. controllers

I have a big app, which contains many layouts and subviews.
Looks like (simplified):
http://plnkr.co/edit/x4kleCCQH5Hsy6dcjgXe?p=preview
So, I have many directives and services. And only 2-4 simple controllers (10 - 20 lines of code). All logic stored into big directives, which contain many small directives. Am I wrong?
The way I'm approaching it is.
Directives contain view logic, not business logic. This is also where DOM-messing about happens if needed.
Controllers are fairly thin, have minimal business logic.
Angular Services are where most of the heavy lifting is done.
If you have logic that needs to be re-used by multiple areas/controllers or is stateful - it's probably a better fit to put into a Service than a Directive.
Depending on your app/architecture - you could be posting to a server, and having the heavy BL happen on the server side.
The Angular docs have a nice bit on Using Directives Correctly that has a few pointers.
Sounds good to me.
At some point in the DoubleClick talk, the speaker says something similar to "make controllers as thin as possible."
The link was busted... a working link to the same guide is here:
http://docs.angularjs.org/guide/controller

Resources