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

Bindings from parent->child work differently for object #1056

Closed
oliver-batchelor opened this issue Jun 14, 2012 · 7 comments
Closed

Bindings from parent->child work differently for object #1056

oliver-batchelor opened this issue Jun 14, 2012 · 7 comments

Comments

@oliver-batchelor
Copy link

Using a transclude directive we have

  1. A binding to a value 'someVar'
  2. A binding to a value in an object 'data.someVar'

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/

<wrap title="set someVar">
  <input type="text" ng-model="someVar">
  <p>Inside wrap: {{ someVar }} </p>
</wrap>
<p>Outside wrap: {{ someVar }}</p>   

C1 = function ($scope){
$scope.someVar = "Hello world";
$scope.data = {someVar: "Hello world"};
};

@mhevery
Copy link
Contributor

mhevery commented Jun 14, 2012

This is working as expected. Transclusion creates new scope. Doing 2 way data-binding on scope is not recommended specifically because of such behavior.

@mhevery mhevery closed this as completed Jun 14, 2012
@witoldsz
Copy link

Hi Misko,
can you elaborate a little bit more on the problem? You say it is not recommended to do a 2-way data binding on a scope, but there is no binding to the scope in the example, is there?

Here is the discussion on the subject:
https://groups.google.com/forum/#!topic/angular/45jNmQucSCE/discussion

And this a simplified example with description how to reproduce the issue:
http://jsfiddle.net/witoldsz/pnSNj/14/

@mhevery
Copy link
Contributor

mhevery commented Jun 15, 2012

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

@witoldsz
Copy link

I see, it is related to #943, isn't it?
If so, would that be possible to let some directives work in a way that the inner part of a directive would not create a transcluded scope (which inherit from outer scope), but use the outer one instead? Like this:
-- this is scope X
<some-directive>
-- we are inside directive
-- this is scope X as well
</some-directive>

@nicola
Copy link

nicola commented Jan 29, 2013

So how can this be made possible?
(that scopes of transcludes influence the parent scope)

@witoldsz
Copy link

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.

@iammerrick
Copy link
Contributor

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?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants