Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seems that we have some stomping of $scope when creating Modal with $modal #140

Closed
neosavvy opened this issue Nov 5, 2014 · 3 comments
Closed

Comments

@neosavvy
Copy link

neosavvy commented Nov 5, 2014

I have a plunkr where I forked the example here: http://plnkr.co/edit/duiV2jSrxWHMhDYTiOLs?p=preview

Seems that if I create a property named someProperty and then create an input with ng-model assigned to it, I end up with a different scope.

I am outputting the $scope.$id value in the UI based on the one available for binding, then console.log'ing the $scope.$id in the controller instance for the modal and the caller.

Any clue as to why the UI has a new scope in this instance, seems that the expected behavior would be to use the $scope of the Modal's instance rather than a new one. Please let me know if I am doing something out of the norm here that isn't desired use.

@jbrowning
Copy link
Member

This is a common issue and is actually related to Angular, not angular-foundation. What I believe you're experiencing is the sibling scope created by ngTransclude. Please see:

The solution is to bind to properties of a model object not individual objects. For example, within your modal template, instead of:

<form>
  <input type="text" ng-model="someProperty">
</form>

We should do:

Parent controller

.controller("someController", ["$scope", function($scope) {
  $scope.someObject = {}
}]

Modal template

<form>
  <input type="text" ng-model="someObject.someProperty">
</form>

Please reopen if this does not describe your issue.

@tamakisquare
Copy link

@neosavvy

@jbrowning is right. What you are seeing is not so much an issue but the characteristics of angular's scope (javascript object, in precise) prototypal inheritance.

Once you get the essence of the scope prototypal inheritance, then you should get familiar with the scopes (yes, plural) involved in a modal. The topmost scope of a modal is what you have been working with in your controller (scope A). Under it, there's an isolated scope for modalWindow (scope B), which contains a transcluded scope (scope C). It's scope C that your modal template lives in and thus is what ng-model='someProperty' binds to, while you $watch for someProperty in scope A (which never get triggered).

@neosavvy
Copy link
Author

neosavvy commented Nov 5, 2014

My colleague actually helped solve this issue and it should have been obvious to me. http://plnkr.co/edit/uMGwaGbJWc7bP7Ar8jSA?p=preview

The above plunkr has the solution.

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

No branches or pull requests

3 participants