bootstrap icon indicating copy to clipboard operation
bootstrap copied to clipboard

[bootstrap3_bis] Form in modal has scope issue

Open mvhecke opened this issue 13 years ago • 14 comments

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

mvhecke avatar Aug 26 '13 14:08 mvhecke

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 avatar Aug 27 '13 08:08 Narretz

@Narretz Is there any 'proper' solution to get the form object instead or do we have to wait for a stable release?

wcoppens avatar Aug 27 '13 08:08 wcoppens

@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.

karol-f avatar Aug 27 '13 14:08 karol-f

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.

mvhecke avatar Aug 27 '13 14:08 mvhecke

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.

karol-f avatar Aug 27 '13 14:08 karol-f

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'
    });

mvhecke avatar Aug 27 '13 14:08 mvhecke

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.

karol-f avatar Aug 27 '13 14:08 karol-f

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.

Narretz avatar Aug 28 '13 08:08 Narretz

Thank you @Narretz! This seems to solve my problems. I would also like to thank @blnight.

mvhecke avatar Aug 28 '13 09:08 mvhecke

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.

AGiorgetti avatar Sep 09 '13 14:09 AGiorgetti

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?

BastianVoigt avatar Sep 20 '13 14:09 BastianVoigt

I will reopen it @batzee, in most cases this workaround is sufficient although in some cases it won't.

mvhecke avatar Sep 20 '13 14:09 mvhecke

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

BastianVoigt avatar Sep 23 '13 10:09 BastianVoigt

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...

pkozlowski-opensource avatar Sep 23 '13 10:09 pkozlowski-opensource