r/javascript • • Apr 07 '17

Opinionated Comparison of React, Angular2, and Aurelia

https://github.com/stickfigure/blog/wiki/Opinionated-Comparison-of-React%2C-Angular2%2C-and-Aurelia
59 Upvotes

55 comments sorted by

View all comments

Show parent comments

2

u/[deleted] Apr 07 '17

If we're being honest, Angular has DI as an emergency hatch because it has none of the natural means above.

That's very eloquently put. In most cases I've seen DI integrated, the framework is doing it because it insists on taking away control from users in how they create some of their objects, so then the framework itself has to provide some convoluted means of doing DI.

Unfortunately this flaw is then put on feature bullet lists as a strength of the framework. "It has built-in DI!"

1

u/tme321 Apr 08 '17

That's also downright false. If I want to inject an object manually into a component in angular I'm free to do so as well. I can pass any object I want in through standard input bindings.

I can define some interface:

interface DoesFoo {
    foo: ()=>null;
}

I can define a component with some sort of dependency field as a member of the controller:

@Component({selector:'comp'})
class Comp { 
    @Input() myDep: DoesFoo;

    ngOnInit() {
        this.myDep.foo();
    }
}

And then another component can pass whatever it wants in when it uses the conponent:

<comp [myDep]="someObjectWithFoo"></comp>

Sure, you don't use the spread operator syntax. But that doesn't fundamentally change how you can pass stuff around. That's just some syntactically sugar.

2

u/[deleted] Apr 08 '17

While this is injection, typically you wouldn't be passing most of your dependencies through component attributes, every time you use one, this is why I ended my example here about DI in React and JS in general with an example that doesn't require passing through attributes.

Now you tell me if this approach I describe there, as basic as it is, is practical in Angular.

2

u/tme321 Apr 08 '17

Don't misunderstand me. I would pretty much never do this. But I'm just saying Angular doesn't force DI in the constructor because "hurr durr it's so poorly thought out that it can't do it any other way".

I wouldn't do this. But angular isn't forcing people into as much as a lot of devs around here seem to think. Because they never use it but love to make conjecture about it.

And your example is worthless in the context of angular because it has a dependency injection system. Which you would just use instead of using your method above. All your method is doing is manual di on every instance of the component.

2

u/[deleted] Apr 08 '17

Ok, I'm waiting for your example how to do it, which is not "hurr durr" though.

Because what you're suggesting is, by your own admission, kinda useless as a way of doing DI.

1

u/tme321 Apr 08 '17

I'm not going to retype the angular documentation. Your free to go to angular.io and read about how to use the di system there any time you want.

2

u/[deleted] Apr 08 '17

If I can write a five line example about how to do this in React, which also happens to be general purpose JavaScript... and you are pointing me to the Angular documentation, then this by definition means you're saying the general purpose approach doesn't work in Angular.

Which was the the statement I made, and you were trying to disprove. So, are we on the same page now?

→ More replies (0)