Nithin P.H
Nithin P.H

Reputation: 722

Passing data between controllers in uib modal

I have written a common uib modal in my angularjs app. The content of its body and footer needs to change according to some situations so I have given it like this as of now. The code is actually working fine but need to make sure if Im doing things the right way...

Here on clicking a button of modal I'm actually comparing the text of the button clicked. I'm not expecting the modal as a directive.

HTML code

<div class="modal-header" >
   <button type="button" class="close" ng-click="close()" data-dismiss="modal">&times;</button>
   <h4><b>{{customModal.title}}</b></h4>
</div>
<div class="modal-body">
 {{customModal.body}}
</div>
<div class="modal-footer" >  
   <span ng-repeat="item in customModal.buttons">
   <button type="button" class="{{item.btnClass}}" ng-click="modalBtnClick(customModal,item);close()" data-dismiss="modal" ng-if="item.show">{{item.text}}</button>
   <span>
</div>

Calls the modal on clicking.....

$scope.areYouSureModalInstance = $uibModal.open({
  animation: true,
  templateUrl: 'views/Modal.html',
  controller: 'ModalCtrl',
  windowClass: 'nested-modal',
  scope: $scope,
  resolve: {
    items: function() {
      let btns=[
          {id:1,text:"Yes",show:true, btnClass:"btn btn-success"},
          {id:2,text:"No",show:true, btnClass:"btn btn-danger"},
          {id:3,text:"Cancel",show:true, btnClass:"btn btn-default"}
        ];
      $scope.customModal = {
        id:'confirm',
        title:'Confirm',
        body:'Do you want to save the changes you have made?',
        buttons:btns
      };
      //passes the modal properties
      return $scope.customModal;
    }
  }
});

 //Inside the uib modal controller

   function ModalCtrl($scope) {

    $scope.close = function() {
      $scope.areYouSureModalInstance.close();
    };

     $scope.modalBtnClick=function(data,btn){
      if(data.id==="confirm"){
       $scope.$emit('close.confirm',btn);
      }
   };
 }


//If the user clicks on any button



$scope.$on('close.confirm', function(event, data) {

if (data.text.toLowerCase()==="no") {
  //do some stuff here
 }
if (data.text.toLowerCase()==="yes") {
  //do some stuff here
}
$scope.modalInstance.close();
});

Is there a better way to do this with two controller and not using the modal a directive...

Upvotes: 0

Views: 832

Answers (1)

John Velasquez
John Velasquez

Reputation: 3451

$uibModal.open().result returns a Promise.

So therefore subscribe to the promise result by doing

$scope.areYouSureModalInstance.then(function(args) {
  //arguments that the modal returned
  console.log(args);
},function () {
  //errors
});

for closing and confirmation method

 function ModalCtrl($scope, $uibModalInstance) {

 $scope.close = function() {
   $uibModalInstance.dismiss();
 };

 $scope.modalBtnClick=function(data,btn){
  if(data.id==="confirm"){
   // btn -> arguments that you pass
   $uibModalInstance.close(btn);
  }
 };
}

Upvotes: 1

Related Questions