-
Notifications
You must be signed in to change notification settings - Fork 27.5k
Bindings from parent->child work differently for object #1056
Comments
This is working as expected. Transclusion creates new scope. Doing 2 way data-binding on scope is not recommended specifically because of such behavior. |
Hi Misko, Here is the discussion on the subject: And this a simplified example with description how to reproduce the issue: |
The above is an issue, since it binds directly to the scope. The issue is when you insert a scope creating directive in around the input. This does not suffer from the same issue. Since the model will always be read from the right scope and then the assignment will happen always on the same instance of the model. No matter how many scopes you wrap the input in it will work as expected. Example contains ng-model hence 2way data binidng |
I see, it is related to #943, isn't it? |
So how can this be made possible? |
As Misko said, you are out of troubles once you stop binding directly to scope. This is not the way the AngularJS was specifically designed, this is how JavaScript inheritance works. |
When people say not to bind directly to scopes isn't that exactly what a two way data binding is? Or do you mean don't bind on the $scope because objects are references therefore for consistent behavior always bind to an object that is on the scope? |
Using a transclude directive we have
We have a textbox inside a transclude which is bound, and bind the value inside and outside the transclude for display.
In the first case we lose the binding between the two scopes as soon as we type text into the text box.
In the second case the binding is retained.
http://jsfiddle.net/Saulzar/pnSNj/12/
C1 = function ($scope){
$scope.someVar = "Hello world";
$scope.data = {someVar: "Hello world"};
};
The text was updated successfully, but these errors were encountered: