Why does angularjs need IoC/DI?

est

Following this question

https://stackoverflow.com/a/2465052/41948

So Python doesn't need IoC/DI because it's dynamic scripting language already.

Javascript is also a dynamic scripting langauge, why does angularjs need DI then?

Is it because JSON <-> DOM is static? Could someone give me a minimal example?

Brian Genisio

Dependency Injection (DI) in Angular wasn't a necessary decision. Most other JavaScript frameworks don't have it built in. (Although look at Marionette, a framework built on top of Backbone.js... it includes an optional DI layer). Angular.js comes with a set of architectural opinions that help you separate your code. It was built in as a design decision, not a necessity.

The biggest reason it is necessary for YOU to use DI in Angular is because that is the way Angular works. The angular team could have decided to use an Asynchronous Module Definition (AMD) library like Require.js. Instead they chose a DI pattern and baked it in for convenience.

The link you posted suggests that DI might be an anti-pattern in dynamic languages. I disagree with that. I'd just say that DI is less necessary in dynamic languages. In the case of Angular, however, it works well. They allow you to compose your system of parts and inject only what you need, when you need it. When you look at the way other frameworks do it, they (often) just namespace their Model/View/Controller/Template/Router parts in the global space (Like App.Models.Person in Backbone.js).

I find DI in Angular to be like "When in Rome, do as the Romans do". Embrace the architectural decision. It feels good.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does angularjs need IoC/DI?

From Dev

Why does angularJS need to run a server?

From Dev

What is the benefit of having $sce or Strict Contextual Escaping in angularjs and why react does not need to do it?

From Dev

Why does a boolean need to be atomic?

From Dev

Why does Angular need wrappers?

From Dev

Why does LayoutInflater need a context?

From Dev

Why does sequenceA need Traversable?

From Dev

Why does Idris need mutual?

From Dev

Why does this need an explicit type?

From Dev

Why does windows need withSocketsDo?

From Dev

Why does this clojure macro need `'~?

From Dev

Why does this variable need to be static?

From Dev

Why does a boolean need to be atomic?

From Dev

Why does gedit need libbluray?

From Dev

Why does Linux need SELinux

From Dev

Why does this angularjs directive not work?

From Dev

Why does this angularjs directive not work?

From Dev

Why does StructuredArray need to be non-constructible?

From Dev

Why does alien need sudo privilege?

From Dev

Why does the compiler need that trait hint?

From Dev

Logrotate: "log does not need rotating" why?

From Dev

Why does this code need the volatile keyword?

From Dev

Why does Angular Controller need "$scope"

From Dev

Why does Angular not need a dash in component name

From Dev

Why does Redshift not need materialized views or indexes?

From Dev

Why does Rust need the `if let` syntax?

From Dev

Why does initWithCoder need self.property?

From Dev

Why does the 'bin' user need a login shell?

From Dev

Why does primefaces menuitem need a form?