模态,使用解析绑定传递的变量

时间:2015-12-08 13:52:51

标签: angularjs angularjs-directive bootstrap-modal

我有以下指令

angular.module('ui.bootstrap.demo').directive('warningDirective',function() {
  return {
    restrict: 'EA',
    scope: {
      showwarning: '=',
      warningmessage: '=',
    },
    controller: function ($scope, $uibModal, $element) {
      $scope.$watch('showwarning', function (newVal, oldVal) {
        if (angular.isDefined(newVal)  && newVal != oldVal && newVal) {
          $scope.open();
        }
      });
      $scope.open = function () {
        var modalInstance = $uibModal.open({
          templateUrl: 'warningModal.html',
          resolve: {
            warningMessage: function() {
              return $scope.warningmessage;
            },
            showWarning: function() {
              return $scope.showwarning;
            }
          },
          windowClass: 'warning-modal',
          controller: function ($scope, $modalInstance, warningMessage, showWarning) {
            $scope.warningMessage = warningMessage;
            $scope.showWarning = showWarning;
            $scope.confirmWarning = function () {
              //action here
              $scope.showWarning = false;
              $modalInstance.dismiss('cancel');
            }
            $scope.closeModal = function () {
              $scope.showWarning = false;
              $modalInstance.dismiss('cancel');
            }
          }
        });
      }
    }
  }
})

这是一个可重复使用的指令,用于显示模态警告(稍后我会传递一个yes动作函数)。在指令内部,我正在观察范围变量showwarning,当它显示弹出窗口时为真。我正在使用resolve传递它,并在用户关闭模式或单击是时将其设置为false

我的问题是它没有得到更新。稍后当我在控制器中将showwarning设置为true时,由于showwarning仍为true,因此无法触发观看。

这是a plunker

模态只显示一次

1 个答案:

答案 0 :(得分:0)

只要模态关闭,您只需将showwarning更改为false。

<强> DEMO

$scope.open = function() {
  var modalInstance = $uibModal.open({
    // modal options
  });

  // set showwarning to false
  modalInstance.result.finally(function() {
    $scope.showwarning = false;
  });

};