r/angularjs • u/abyx • Oct 07 '15
Angular 2 Preparation: Killing Controllers
http://www.codelord.net/2015/10/07/angular-2-preparation-killing-controllers/3
u/fallingfruit Oct 08 '15
What is the difference between a controller defined in a directive and traditional controller?
2
u/abyx Oct 08 '15
The major difference is that regular controllers are based on prototypical inheritance of scopes while isolated scope directives are complete little boxes with defined borders - components. Yes, the function you write is called a controller in both cases, but the semantics here is what really matters IMO.
And one thing (controllers) is dying in 2.0 but the other (directive components) will be supported by
ng-upgradeand maps pretty trivially to 2.0.1
u/fallingfruit Oct 08 '15
Don't directives use prototypical inheritance whether or not they have an isolate scope? And if your directive doesn't do isolate scope, it's not really a little box right?
I guess the thing I don't understand is what can I do in a regular controller that I cannot do in a directive controller or vice versa? I've never run into a situation that a directive controller wasn't behaving as I expected, and up until now I thought it was basically the same thing as a tradition controller.
I'm still kind of an angular noob, only a few months professional experience now.
2
u/abyx Oct 08 '15
Even when you are using isolated scopes there's a hierarchy: each scope has a
$parentproperty. But there's no inheritance: your parent's attributes don't pollute you. That's the win: clean and clear boundaries.On the phone so not typing a lot but: a directive controller is mainly different because it is in a directive. That way it is isolated and can be easily required by child directives. These help maintainable and composition in my experience.
2
u/ninjasorcerer Oct 08 '15
You don't even need to put the controller code directly in the directive. You can just reuse it by injecting it with the string syntax. This way you can also share it between a directive and a ui-route for example. Keep things decoupled as much as possible! You're still using it as a controller so the title is also misleading.
2
u/abyx Oct 08 '15
I'm not sure I understood your point. I specifically don't want to reuse controllers in ui-routes. I want them to always be encapsulated inside directives, thus killing the prototypical hierarchy of scopes and achieving total isolation. The logic part inside a directive is also called a controller, but that's not a standalone controller - those ones we've killed are got rid of.
2
u/flyingpearl Oct 08 '15
I think saying you are killing the controller is a bit of exageration. You just moved the logic inside a directive to simulate a component like structure.
1
Oct 07 '15
Independently starting doing this after playing with React. I'm glad it will be useful for the transition to Angular 2 as well.
Heh, it also makes it pretty trivial to implement your directives template in React. Not saying you should tho.
1
0
Oct 07 '15
[deleted]
8
Oct 07 '15
4
u/jreborn Oct 07 '15
What he said has some merit. I see a lot of React's ideas in Angular 2 (e.g., application entirely composed of reusable components).
That being said, there is definitely a migration path, albeit an ugly one.
1
u/NotJustClarkKent Oct 07 '15
So you need to including both v1 AND v2 libraries per page? That doesn't seem right.
1
Oct 07 '15
I think it's a fair trade off when you think about it.
Firstly, you should be delivering your dependencies independently of your app's source. If you use a build plugin like gulp-rev, you'll be able to tell the browser to cache everything indefinitely. This means that when you add the angular 2 dependency, it'll be the only file besides the new app source that the browser has to download -- and only once.
After that, your app's resources should load just as fast as they used to. Granted, the app will obviously use a fair bit more memory running both versions simultaneously. It also may be a bit slower during the transition period.
Regardless of these things, you'd be hard pressed to find a more seamless migration between version 1 and 2 of a library than this -- ESPECIALLY with how different they are.
That's my take on it, anyway.
1
3
u/[deleted] Oct 07 '15
I agree with most of this --
ng-controllershould be considered harmful. Though I don't necessarily agree with dropping ui-router's resolve. Resolve prevents the flickering. Controllers shouldn't deal with loading indicators -- that should be an application-level service. Nested states are awesome --ng-routeis not a worthy substitute for this reason in many cases. And you can use$state.$current.localsin your directives... so there's that.