[bootstrap3_bis] Form in modal has scope issue
I was trying out the latest version of the new modal and noticed it had a problem with handling form validation. When trying to validate I couldn't find my form in the scope, but I found out it is available through the $$childTail. As far as I know it's bad practice to access $$childTail and other with multiple $. It seems like a scoping issue, but so far I'm unable to fix it.
Below a link to the Plunk, where I used the bootstrap3_bis branch: http://plnkr.co/edit/pkQNcC?p=preview
I guess that's because the new modal uses a directive called modal-window for the actual modal. So your form is inside the modal directive, and thus not directly available in the parent scope.
@Narretz Is there any 'proper' solution to get the form object instead or do we have to wait for a stable release?
@wcoppens In modal you can set your desired $scope - https://github.com/angular-ui/bootstrap/tree/master/src/modal/docs . I hope this will help. Regards.
I've noticed also that this option @blnight, though it isn't possible when dealing with form validation. As far is I know of course.
Usually when I have problem with sharing object using directive with transcluded HTML usually the solution is simply use controller in directive that helps keeping object in scope (directive, transcluded HTML and even parent scope if I use "dot" notation in model).
However I didn't analyze this problem so sorry if I put you to wrong direction.
Regards.
Thank for your answer @blnight, normally this would be the solution I think. When setting the modal options like below I expect to find my testform (that was set in the tempalte) in scope I use in my controller.
$modal.open({
backdrop: true,
keyboard: true,
templateUrl: 'my-template.html',
controller: 'MyCtrl'
});
I don't feel comfortable to guide you in this as I didn't test it but please try this:
$modal.open({
backdrop: true,
keyboard: true,
templateUrl: 'my-template.html',
controller: 'MyCtrl',
scope: $scope
});
If this won't help I hope someone will give you better solutions. Regards.
There's also the option to pass the form to the submit function:
<form name="testform" ng-submit="submit(testform)" novalidate="">
$scope.submit = function (testform) {
console.log(testform);
};
angular validation is generally not built in mind with explicitly accessing the form your controller.
Thank you @Narretz! This seems to solve my problems. I would also like to thank @blnight.
I'm facing this problem too, in some places I could not easily pass the 'form' as parameter of the functions.. so I had to mess around with the scopes to find the right one.
Hey @Gamemaniak, I have the same problem you describe, I'm using the stable build 0.6.0.
Why did you close this issue? The ng-submit="submit(testform)" looks more like a workaround to me than a real solution. Shouldn't this be addressed as a bug?
I will reopen it @batzee, in most cases this workaround is sufficient although in some cases it won't.
I'll try to give some more detail on what goes wrong: $modal.open() creates two nested scopes instead of one, and passes the outer scope to the controller, while the directives used in the modal template are passed the inner scope. This is why the form cannot be found in the controller's scope but in $$childTail.
Haven't yet found out where the second new scope is created though. Still investigating
This is duplicate of https://github.com/angular-ui/bootstrap/issues/969. Additional scope is created by the transclusion in the modalWindow directive. See #969 for more details. A "fix" for this situation is not trivial I'm afraid, AngularJS scopes can be confusing at times...