-
Notifications
You must be signed in to change notification settings - Fork 27.5k
Expose isolated scope binding API #8338
Comments
@Izhaki - I would like to know what scenarios other than An alternative approach for non-
|
Well... the obvious other common scenario is when you which to pass parameters in one config object, rather than many attributes, like so:
Note that this will also affect isolated scopes (that wish to have binding to models provided in a config object), not just child scopes. Anyhow, the proposed solution seems right, but it doesn't work if the parent and child scope both call the model in the same way ( This, however, seems to work fine:
|
I had further look into this, and the feature requested cannot be that easily implemented. Reasons are:
Anyhow, for two-way binding (
and clients just call:
But this will only work if the scope in question is a child or isolated scope. As I've seen similar requests/questions to mine on the net, perhaps it is worth considering moving the two-way-binding code to Scope, and using it from the Or, what I believe in terms of software-architecture is the proper solution, is Scope to offer I can provide more details and even a provisional PR to show exactly how I envision this to work. |
Although not in context, a minor change to the code above will also allow a client to do this:
Might be handy? |
I think the conclusion here is that this is not really possible / wanted in the AngularJS scope system, as one element can only have type of scope. Exposing the isolate scope api would muddle this distinction. However, with bindToController, you can achieve this at least when not using In, the ngModel example, you can also bind ngModel to an object, and you'll get binding updates via $onChange and two way binding via object reference at the same time: http://plnkr.co/edit/JKbOa2ieZEeRFxg1sL5t?p=preview |
This is a feature request.
The issue
There are various scenarios where one would like to bind models on a child scope (
scope: true
) like on an isolated scope (scope: {}
).For instance consider the following:
The
typeahead
directive would normally usengModelController
to deal with the model. But sometimes it would like to bound the model to its scope, so to delegate it to sub-directives (in its template).If the
typeahead
directive uses an isolated scope, it can easily bind to the model using:But the directive may wish to have a child scope so to see many related models on the parent directive, and there isn't a straight-forward way to bind to the model provided by
ngModel
.Ways to achieve this
There are various ways to achieve this:
Via ngModelController
One can use the input formatters with $parse to get and set the model provided. But this will only work for
ngModel
.Like ngModelController
One can take the same route as
ngModelController
:Double watching
As anagelc proposes in this issue one can do something like this:
But depending on the model, this may fail.
Using a custom service
Another solution would be similar to that proposed by basarat on the same issue as above - a custom service, which essentially exposes code currently in $compile.
Exposing the bind API.
The question is, why not just expose the same methods in $compile used for isolated scope binding to all, essentially allowing us to write something like this:
The text was updated successfully, but these errors were encountered: