在 uib 模式中的控制器之间传递数据

Passing data between controllers in uib modal

我在我的 angularjs 应用程序中写了一个通用的 uib modal。它的正文和页脚的内容需要根据某些情况进行更改,所以我现在这样给它。该代码实际上工作正常,但需要确保我是否以正确的方式做事...

这里点击模态按钮实际上是在比较所点击按钮的文本。 我不希望模态作为指令。

HTML代码

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

点击时调用模式......

$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();
});

有没有更好的方法来使用两个控制器而不使用模态 a 指令...

$uibModal.open().resultreturns一个承诺。

因此通过

订阅承诺结果
$scope.areYouSureModalInstance.then(function(args) {
  //arguments that the modal returned
  console.log(args);
},function () {
  //errors
});

关闭和确认方法

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