0

I am trying to automatically check a checkbox when a modal closes. I am using one controller for the modal and another controller for the main page. The code below isn't working. Do I need to use a directive in order to accomplish this, or is there another way?

HTML - Main Page:

<label>
  <input type="checkbox" ng-model="agreementForm.value1"> I agree.
</label> 

HTML - Modal:

<div class="modal-footer">
 <button ng-click="agreementForm.cancel()" class="btn btn-warning">Cancel</button>
  <button ng-click="agreementForm.agree()" ng-disabled="agreementForm.$invalid" class="btn btn-primary" >I agree</button>
</div>

Javascript for Controllers:

myApp.controller('AgreementFormCtrl', function ($location, $stateParams, $modalInstance) {
  var agreementForm = this;
  agreementForm.cancel = function () {
  $modalInstance.dismiss('cancel');
};

agreementForm.agree = function() {
  agreementForm.value1=true;
  $modalInstance.close(agreementForm.selected);
});

myApp.controller('ContactFormCtrl',
   function ($location, $stateParams, Contacts) {
     var contactForm = this;
  });
   contactForm.save = function () {
     Contacts.$add(contactForm.contact).then(function (data) {
       $location.path('/');
     });
   };

Router for Modal:

.state('payment.agreement', {
url: '/agreement',
onEnter: ['$stateParams', '$state', '$modal', function ($stateParams, $state, $modal) {
   $modal.open({
       templateUrl: 'views/agreement.html',
       controller: 'AgreementFormCtrl as agreementForm'
      }
    )
      .result.finally(function () {
        $state.go('^');
      });
  }]
})
2
  • 1
    Are you sure the agreementForm.agree() function is actually being called and executes without error? Bootstrap UI modals have some scope considerations which may be the problem here, see stackoverflow.com/questions/18924577/… Commented Mar 11, 2015 at 8:12
  • @Fasermaler Thanks for the link. I think agreementForm.agree() is working OK. When I click it, the modal closes. And I'm not seeing any errors in my console. Commented Mar 11, 2015 at 8:18

1 Answer 1

1

your can do it by using $rootScope and also you missed to initialzed the $scope

First you need to add ng-checked directive in the checkbox

 <input type="checkbox" **ng-checked="agreementForm.IsChecked"**
  ng-model="agreementForm.value1"> I agree.

Then you need initialize the rootscope in your controller

myApp.controller('AgreementFormCtrl', function ($location, $stateParams, $modalInstance,**$rootScope,$scope**) { //code };

and finally you can assign the value for checked object when a modal closes

$scope.agreementForm.cancel = function () {
  $modalInstance.dismiss('cancel');
$rootScope.agreementForm.IsChecked="Checked";//True or false
};
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you. I've heard that it's a bad idea to use rootScope unless absolutely necessary. Is it OK to use in this instance? As you can see, I'm still learning...
I need to type faster, I was asking the same thing of Ramesh. Obviously just doing stuff in root scope will solve any problems with the scope hierarchy you may have, but I think you should also be able to do it without this rather drastic alternative.
I think your problem will be solved with using rootscope. It's not good. but if you called a model object to other controler, it might me used.
@Ramesh Rajendran I didn't use $scope because I'm trying to use the As Controller syntax. Did I use it incorrectly?
if you using angularjs,then the variables are you should be $scope variables
|

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.