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

feat($compile): allow using bindToController as object, support both new/isolate scopes #10467

Closed
wants to merge 4 commits into from

Conversation

caitp
Copy link
Contributor

@caitp caitp commented Dec 15, 2014

bindToController is now able to be specified as a convenient object notation:

bindToController: {
  text: '@text',
  obj: '=obj',
  expr: '&expr'
},
scope: {
  notBoundToCtrl: '@someOtherAttr'
}

It can also be used in conjunction with new scopes, rather than exclusively isolate scopes:

bindToController: {
  text: '@text',
  obj: '=obj',
  expr: '&expr'
},
scope: true

PING @petebacondarwin / @johnlindquist

I am not super keen on this, because it makes things much crazier than they used to be. That said:

  • Currently, bindToController does not automatically imply an isolate scope --- maybe it should (I hate this though)
  • It could probably do with more errors being thrown when people use it wrong
  • The errors it does throw could be better
  • The errors it throws are potentially breaking changes :(

Closes #10420

@googlebot
Copy link

CLAs look good, thanks!

@johnlindquist
Copy link
Contributor

Why do you hate implying an isolate scope?

Also, what are the goals of "bindToController"?

For example, I'm trying to reconcile bindToController with the tabs example on the http://angularjs.org
http://jsfiddle.net/g6jb1d4b/

and using bindToController in conjunction with "require:'^tabs'" and a linking function bother me a bit. :/

(fyi, I'm working on a pull request to update the angularjs.org homepage examples)

@caitp
Copy link
Contributor Author

caitp commented Dec 15, 2014

The goal of it is really just to make isolate scopes work better with controllerAs --- that's all it was ever intended to do. But, there's no reason it couldn't work with new scopes too, technically.

@caitp
Copy link
Contributor Author

caitp commented Dec 15, 2014

Anyways, it's going to be a while before this (or something like it) lands, so it would be good to get some feedback on it, try it out and see how you like it, find bugs

@johnlindquist
Copy link
Contributor

@caitp ok, thanks.

So if the goal is "scopes work better with controllerAs", have you discussed:

If a "controllerAs" is defined, imply that scope:{} bindings belong on that controller? That way you could remove bindToController completely...

@caitp
Copy link
Contributor Author

caitp commented Dec 15, 2014

We can't really do that, it would be a breaking change

@caitp
Copy link
Contributor Author

caitp commented Dec 15, 2014

Even for 1.4, it's too much of a break --- well, "too much of a break" --- it's more that controllerAs does not imply that you want bindings attached to the controller, it doesn't really make sense to do it that way even if it is a bit simpler.

Obviously, the directive api is stupidly complicated, and making it more complicated isn't helping anyone, so finding ways to simplify this would benefit everyone. But it's also a huge inconvenience due to breaking apps

@petebacondarwin
Copy link
Member

I think the primary goal is to move even more toward the way things will work in Angular 2 where we don't use scope directly for passing data to the template but instead attach stuff to the controller that is exposed on to the template - in 1.x this is via a scope but in 2 the controller instance is the scope

@caitp
Copy link
Contributor Author

caitp commented Dec 15, 2014

neh, the rationale had nothing to do with v2, v2's prototype wasn't even as settled as it is now at that point.

But that's not a very important thing -- the important thing is that people generally like controllerAs and want to use it, and want to be able to put their isolate scope bindings in the controller.

@johnlindquist
Copy link
Contributor

@caitp and @petebacondarwin
I would love to see your take on this famous homepage example updated with bindToController and controllerAs. I left some comments where it feels 'weird':

https://jsbin.com/xugomi/3/edit?html,js,output

@@ -0,0 +1,68 @@
@ngdoc error
@name $compile:noctrl
@fullName Controller is required.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this full name be more like: "controller as" identifier required to bind to controller?

Copy link
Member

@gkalpak gkalpak Jan 19, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I am not mitaken, this error will also be thrown if there is bindToController: true but no controler:.... So the literals have to account for all cases (i.e. bindToController: true but no controller or no controller identifier).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I see that now. I was thrown by the main body of the error document, which doesn't really mention that aspect of the error at all.
I wonder if we ought to have two separate errors?

@petebacondarwin
Copy link
Member

Apart from splitting the error message, this looks good to me. @gkalpak do you have any further comments.

@gkalpak
Copy link
Member

gkalpak commented Jan 19, 2015

👍 for separate error messages.

In the original PR, @caitp mentioned something about potential leaks and performance regressions.
I don't think it would be much of an issue (but I wouldn't know better); do you think it would make sense to run some benchmarks (if not already done so) ?

Other than that it LGTM.

@caitp
Copy link
Contributor Author

caitp commented Jan 19, 2015

if you want to release this today, i'll split up the error message and do whatever other cleanup in about 30 minutes.

@caitp
Copy link
Contributor Author

caitp commented Jan 19, 2015

updated to have 2 different errors


When using the `bindToController` feature of AngularJS, a directive is required
to have a Controller identifier. This can be supplied using the "controllerAs"
property of the directive object, or alternatively by adding " as ALIAS" to the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

directive definition object

@petebacondarwin
Copy link
Member

@caitp - can you take a look at the comments here and update this PR? Thanks

@caitp
Copy link
Contributor Author

caitp commented Jan 27, 2015

I don't think we should be calling it "alias", because it's misleading --- so I'm just going to ignore all of the comments about renaming "identifier" to "alias"

@petebacondarwin
Copy link
Member

@caitp - it is fine for you not to think we should not call it alias. As I explained in this comment, what we do need is consistency.

I assume that your preferred name is identifier. In which case please go through and make sure that this is used throughout. In particular remove any use of alias and label.

@frfancha
Copy link

@caitp
You say "the important thing is that people generally like controllerAs and want to use it".
I wonder why. In our experiences, directives with controller are much slower than directives with just link function and scope access, especially used under ng-repeat

…new/isolate scopes

bindToController is now able to be specified as a convenient object notation:

```
bindToController: {
  text: '@text',
  obj: '=obj',
  expr: '&expr'
},
scope: {}
```

It can also be used in conjunction with new scopes, rather than exclusively isolate scopes:

```
bindToController: {
  text: '@text',
  obj: '=obj',
  expr: '&expr'
},
scope: true
```

Closes angular#10420
The return value of the controller constructor is now respected in all cases.

If controllerAs is used, the controller will be re-bound to scope. If bindToController is used,
the previous binding $watches (if any) will be unwatched, and bindings re-installed on the new
controller.
Clarify that these aliases are identifier names used to reference the controller.
@caitp
Copy link
Contributor Author

caitp commented Jan 29, 2015

this is ready to merge

@petebacondarwin
Copy link
Member

Great! LGTM! Thanks @caitp. I will merge tomorrow unless you want to do it today.

@caitp
Copy link
Contributor Author

caitp commented Jan 29, 2015

I've got it, thanks for reviewing

@caitp caitp closed this in 35498d7 Jan 29, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bindToController should support "scope" syntax/features
6 participants