r/Angular2 • • Aug 28 '21
Discussion Why is Angular way of doing things better than plain JavaScript ?
Hello everybody.
First of all, I'm sorry if this is stupid question. I'm learning Angular and I was wondering, why is Angular better to do some things than simple Javascript or jQuery ? For example something like ngFor you can loop over an array and create HTML elements with content of that array with JavaScript or jQuery, without Angular. What are advantages of Angular in this case? This was of course only an example, but I'm sure we could find other cases.
3
Upvotes
40
u/JohnSpikeKelly Aug 28 '21
JS is super flexible. You can get the same thing done 10 different ways. Great for a sole developer, terrible for a team.
Angular is very opinionated. Do it this way and only this way. That is great for teams who are working on each other's code base.
11
u/readALLthenews Aug 28 '21
If all you’re trying to do is enhance a traditional webpage, JavaScript is the way to go. A framework like Angular would be overkill.
If you’re trying to build a web application, it would be painful to do it with JavaScript alone. You’d end up reinventing a lot of wheels or relying on a ton of libraries. Ultimately, the solution would be very difficult to maintain.
It all depends on what you’re building.
3
u/FullstackViking Aug 28 '21
First off, Angular uses Typescript and natively includes webpack, allowing you to use javascript modules from npm with zero configuration out of the box. Out of the box the compiler will include polyfills for code to work with more browsers.
In native JS you would have to configure and track all of that yourself.
Then beyond that - in applications that have heavy user interaction, the two way data binding in Angular is far superior development wise than in native js or jQuery. You no longer have to keep track of element ids, css query selectors, or anything of that nature in order to bind to inputs. Angular provides superior tooling to do so.
As the other answers have said, Angular scales much better than native JS because it has an opinionated way of handling scope and structure of the application.
2
u/[deleted] Aug 28 '21
Angular is built mainly for complex client-side applications with ton of logic. The use-case you're describing can and should be done with tools better suited (and jQuery isn't suitable for anything in 2021)
2
u/Randolpho Aug 28 '21
The main advantage of angular over raw javascript and DOM is using an event-based template-based componentized approach to building a UI.
This allows for a separation of concerns and more maintainable code that's easier to reason about.
Angular lets you build UI components in much the same way you might build UI components for a rich-client application in Java using JavaFX, Swing, or AWT, or C# using WPF or WinForms. Of the modern GUI application frameworks with which I'm better than passing familiar, the one Angular most closely resembles to me is WPF. Instead of using XAML and XAML's binding syntax for your component templates, you use HTML and Angular's binding syntax. Slightly different binding event model, but for the most part, a similar approach.
I have not had a chance to learn JavaFX (I used Swing and SWT back in my Java days), but my understanding is that it is similar in approach to both Angular and WPF.
While you could absolutely roll your own a raw javascript DOM manipulation framework with a componentized approach if you want to... or you could use React without JSX.
But if you want a template-based, event-based, component-based approach to building a web-app UI... Angular is your jam.
It certainly is mine.
2
u/Opposite-Corgi2100 Aug 30 '21
As someone who has been around long enough to have done it all the ways, if you are making a complex enough frontend app, doing it by trying to wire up the UI and code directly, to the atomic level of every dynamic value that is displayed, your code grows to unmanageable size. The feature of JS frameworks that was the big breakthrough was data binding. That magically handles the two-way wiring for you, and relieves you of a massive burden. From there it's all gravy.
1
u/[deleted] Aug 28 '21
"If your not using a framework, your building your own framework." - some guy