This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat($compile): bind isolate scope properties to controller
It is now possible to ask the $compiler's isolate scope property machinery to bind isolate scope properties to a controller rather than scope itself. This feature requires the use of controllerAs, so that the controller-bound properties may still be referenced from binding expressions in views. The current syntax is to prefix the scope name with a '@', like so: scope: { "myData": "=someData", "myString": "@someInterpolation", "myExpr": "&someExpr" }, controllerAs: "someCtrl", bindtoController: true The putting of properties within the context of the controller will only occur if controllerAs is used for an isolate scope with the `bindToController` property of the directive definition object set to `true`. Closes #7635 Closes #7645
- Loading branch information
Showing
3 changed files
with
194 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5f3f25a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it just me, or is the commit message not quite right when it says:
We'll have to remember that for the changelog
5f3f25a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, derp. yes, forgot to change that part of the message when the api changed
5f3f25a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah. there are two issues:
bindtoController
vsbindToController
5f3f25a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
am I correct in thinking that the way to make use of this is as follows
bindToController: true
to directive definition objectcontrollerAs
property in directive definition objectSomething like:
In this case
data
is not directly accessible on the scope viascope.data
but indirectly through the controller which is on the scope likescope.ctrl.data
, correct?That's what I gathered from reading various issues and such, but the changelog merely points to this commit which doesn't summarize the correct/most recent syntax. Seems like a useful feature, thanks for any clarification.
5f3f25a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@twhitbeck that is correct.
Yes, the commit message was not correctly updated, I think the documentation site should be okay though. If it's not, please file a bug
5f3f25a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@twhitbeck there is one small errror in your example:
should be
5f3f25a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh good eye, I didn't notice that @srigi :) yes, you won't be able to bind to properties in the controller if they're actually in the global object
5f3f25a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would there be any way to not require the controller function? If I just want to use the scope properties in my template, but I have no need of a controller function, with the current implementation, I have to have a controller property (which I assign to angular.noop). Just an extra line of boilerplate that I don't feel is necessary. If there aren't any technical limitations to making the controller optional, I'd be happy to take a stab at a PR.
5f3f25a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps it could be done by adding a
|| angular.noop
to this line but I'm unaware of what would break in this case...5f3f25a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you don't have a controller, why would you want to use
bindToController: true
in the first place? o_o5f3f25a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I said, I want to be able to be consistent in my templates and use
controllerAs: 'vm'
for all of my directives and routes. This way, every template is using the same controller name and it's easy to switch between templates. Also, if I ever decide to add a controller in the future, I don't have to worry about updating the template to usecontrollerAs
.Edit: apologies, I wasn't very clear about my reasons in my first comment.
5f3f25a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that really makes sense :(
5f3f25a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bindToController is basically the switch you flip to turn on binding to the controller instead of the scope --- so you still have a switch to flip, but it shouldn't be a terrible amount of work
5f3f25a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I'll try to be more clear:
with a template-only directive (as opposed to one that adds functionality to the element or model), there is no need for a controller. For the above to work, I have to add
controller: angular.noop
.5f3f25a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, accidentally deleted a comment --- anyways, you could open a feature request for that since it's tiny, but I dunno how I feel about it
5f3f25a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done: #9774