Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Update all documentation examples to the 'controller as' syntax introduced in 1.5 #5076

Closed
naomiblack opened this issue Nov 21, 2013 · 10 comments

Comments

@naomiblack
Copy link
Contributor

If you're working on a doc task related to this issue, please note here to prevent duplication of effort.

See http://docs.angularjs.org/api/ng.directive:ngController for an example of 'controller as' syntax

@Narretz
Copy link
Contributor

Narretz commented Jan 13, 2014

The question is though, why? I haven't used this ever, and from trivial examples it doesn't seem to add anything that can't be done without it. There's even an example in the docs where this leads to non-working code:
http://docs.angularjs.org/api/ngView $route.current.scope.name is always undefined. It works in http://docs.angularjs.org/api/ngRoute.$route which uses normal controller syntax

@petebacondarwin
Copy link
Contributor

@Narretz - internally at Google the "controller as" is now the recommended approach. If there is a bug with using it then we should address that. I agree with you though that it is not my preferred syntax and have never used it in any app that I wrote.

@RichardLitt
Copy link
Contributor

@petebacondarwin Do you know why Google switched to using 'controller as' internally?

@petebacondarwin
Copy link
Contributor

Two reasons, I believe:

  1. In Google they like to use classes to define their controllers so it is easier to provide methods on the controller "class" than to have to manually attach everything to the scope.
  2. "controller as" provides more control over what properties on the scope are changed - a child controller cannot accidentally hide properties provided by a parent controller.

@RichardLitt
Copy link
Contributor

@Narretz I don't see how that is non-working code. As far as I can tell, $scope is never injected into the controllers, so $route.current.scope.name should come up undefined. in ngRoute, the name is bound to scope, which is why it works though. (I'm unsure why there is a delay in doing this, though.)

@petebacondarwin This may not be the right place to ask, but it isn't clear to me how it is easier to provide methods than it would be just to attach everything to the scope, as is the norm for Angular controllers. Am I right in thinking that if 'controller as' is used consistently, this isn't in fact an edge case, but a restructuring of how controllers are used in general?

Can you give an example where a child controller might hide the properties of a parent controller?

More to the point; is there a way to submit a draft PR? I can start working on this, but I'm not entirely sure why one would use 'controller as' at the moment.

@RichardLitt
Copy link
Contributor

@petebacondarwin Thank you, I'll see if I can fashion a PR based on this.

@Narretz
Copy link
Contributor

Narretz commented May 20, 2014

@RichardLitt I see now that this is indeed because $scope is not injected in the example that uses controllerAs syntax. The reference to $route.current.scope.name should simply be removed.

In my opinion, changing all examples to controllerAs syntax is a bigger issue than it seems. controllerAs is a very different approach (not necessarily more complex). There are already many many tutorials etc. on angular out there, and most use $scope injected into controllers. What happens if all examples in the official docs have changed? There'll be a huge mismatch between what people have seen and what is used on the official docs. Don't forget, we'd probably have to change the whole tutorial, too ...

And wasn't there a big discussion about publishing the controller on the scope? Because the whole controller is now accessible. I also remember a reverted commit about private scope fields. Was this ever cleared up?

I say let's start with this in angular 2.0 (if it si going to be the recommended way there), and maybe add more info about this in the current docs.

@petebacondarwin
Copy link
Contributor

+1 IMO

RichardLitt added a commit to RichardLitt/angular.js that referenced this issue May 24, 2014
$route.name.scope.current returns undefined in the docs example,
as scope is never injected into the relevant controller.
Scope doesn't need to be there, so it's best to just remove it.

Suggested in angular#5076:
RichardLitt added a commit to RichardLitt/angular.js that referenced this issue May 24, 2014
$route.name.scope.current returns undefined in the docs example,
as scope is never injected into the relevant controller.
Scope doesn't need to be there, so it's best to just remove it.

Suggested in angular#5076
RichardLitt added a commit to RichardLitt/angular.js that referenced this issue May 24, 2014
$route.name.scope.current returns undefined in the docs example,
as scope is never injected into the relevant controller.
Scope doesn't need to be there, so it's best to just remove it.

Suggested in angular#5076.
btford pushed a commit to btford/angular.js that referenced this issue May 24, 2014
$route.name.scope.current returns undefined in the docs example,
as scope is never injected into the relevant controller.
Scope doesn't need to be there, so it's best to just remove it.

Suggested in angular#5076.
RichardLitt added a commit to RichardLitt/angular.js that referenced this issue May 25, 2014
Using `controller as` in the template is not described well
in the docs, as both `scope` injection and `this` are presented
equally without too much discussion of the advantages of using
either. I added a bit more discussion based on google's internal
style guidelines.

Should close angular#5076 until Angular 2.0 comes out and we refactor
everything.
@RichardLitt
Copy link
Contributor

I added some more documentation based on this discussion in #7591. I agree we shouldn't change all of the docs until Angular 2.0.

RichardLitt added a commit to RichardLitt/angular.js that referenced this issue May 27, 2014
Using `controller as` in the template is not described well
in the docs, as both `scope` injection and `this` are presented
equally without too much discussion of the advantages of using
either. I added a bit more discussion based on google's internal
style guidelines.

Should close angular#5076 until Angular 2.0 comes out and we refactor
everything.
RichardLitt added a commit to RichardLitt/angular.js that referenced this issue May 27, 2014
Using `controller as` in the template is not described well
in the docs, as both `scope` injection and `this` are presented
equally without too much discussion of the advantages of using
either. I added a bit more discussion based on google's internal
style guidelines.

Should close angular#5076 until Angular 2.0 comes out and we refactor
everything.
petebacondarwin pushed a commit that referenced this issue May 27, 2014
Using `controller as` in the template is not described well
in the docs, as both `scope` injection and `this` are presented
equally without too much discussion of the advantages of using
either. I added a bit more discussion based on google's internal
style guidelines.

Closes #7591
Closes #5076 (until Angular 2.0 comes out and we refactor everything)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants