0

I'm using the modal from the Angular fork of Bootstrap 3: http://angular-ui.github.io/bootstrap/#/modal and I'm trying to use a checkbox inside the modal and get its value back in my main controller.

The checkbox value is well bound in the view but not in the controller of the modal. Here's an example of the problem: Plunker

The modal controller:

var ModalInstanceCtrl = function ($scope, $modalInstance) {

  $scope.checked = false;

  $scope.ok = function () {
    $modalInstance.close( $scope.checked);
  };

  $scope.cancel = function () {
    $modalInstance.dismiss('cancel');
  };
};

=> $scope.checked in the ok function doesn't contain the correct value...

The view:

<script type="text/ng-template" id="myModalContent.html">
    <div class="modal-header">
        <h3 class="modal-title">I'm a modal!</h3>
    </div>
    <div class="modal-body">
        <label>
          <input type="checkbox" ng-model="checked"> test
        </label><br/>
        Checked: {{checked}}
    </div>
    <div class="modal-footer">
        <button class="btn btn-primary" ng-click="ok()">OK</button>
        <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
    </div>
</script>

Any idea?

Thanks by advance

4
  • It doesn't seem like calling $modalInstance.close with a boolean works very well. If I put a String in there it works. No idea why. Commented May 27, 2014 at 9:57
  • I'm not sure the problem comes from that, I tried to use String inside the checkbox and it still doesn't work: plnkr.co/edit/uY44SiNdcI2Atp6pVWhq?p=preview Commented May 27, 2014 at 10:03
  • That's not what I meant. If you do $modalInstance.close('Hi, I am a String'); that works. See yarons, answer where he uses an object instead of a boolean. That's working around this exact problem. Commented May 27, 2014 at 10:21
  • You should probably ask the maintainers of UI Bootstrap if this is a bug or a feature. The docs does not say anything about using boolean primitives in the close function. Commented May 27, 2014 at 10:22

2 Answers 2

2

A dirty trick that works is to put the checked parameter inside an object.

$scope.checked = {c:false};

$scope.ok = function () {
  $modalInstance.close( $scope.checked.c);
};
Sign up to request clarification or add additional context in comments.

1 Comment

Indeed this works! I accept the answer even if I think this is not working as it should... :/
0

you can specify the checked value straight as a parameter of the function, this is a little workaround for bypassing the lack of the binding between the modal view and the associated controller:

<button class="btn btn-primary" ng-click="ok(checked)">OK</button>

And then pass it as function parameter in the controller:

$scope.ok = function (result) {
       $modalInstance.close( result);
};

Full working example here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.