简体   繁体   English

使模态依赖函数的最简洁方法? AngularJS

[英]cleanest way to make a modal dependent on a function? AngularJS

I have a modal that gets displayed on a click of a button, when that button is clicked it also triggers a couple of javascript functions. 我有一个模式,单击一个按钮后就会显示,单击该按钮时还会触发几个javascript函数。 is there a way to prevent the modal from displaying if one of the functions is false? 如果其中一个函数为假,是否有办法防止模式显示?

<form ng-submit="checkValues(); makeChange();" class="form-inline">
    <div class="form-group">
        <input type="text" ng-model="newChange.amount" maxlength="15" class="form-control"/>
        <button type="submit" data-toggle="modal" data-target="#myModal" class="btn btn-default" >Go</button>
    </div>
</form>

you should use $modal service of angular-ui-bootstrap. 您应该使用angular-ui-bootstrap的$ modal服务。 so you can check if conditions are met and programatically open your modal. 因此您可以检查条件是否满足,并以编程方式打开模态。

here is a sample 这是一个样本

<form ng-submit="checkValues();" class="form-inline">
    <div class="form-group">
        <input type="text" ng-model="newChange.amount" maxlength="15" class="form-control"/>
        <button type="submit" class="btn btn-default" >Go</button>
    </div>
</form>

scope.checkValues = {
    if(!everything.ok){
        //error message maybe
        return;
    }

    var modalInstance = $modal.open({

      templateUrl: 'myModalContent.html',
      controller: 'ModalInstanceCtrl',
      resolve: {
        items: function () {
          return items;
      }
     }
    });

    modalInstance.result.then(function (selectedItem) {
      $scope.selected = selectedItem;
     });
    };

note: if you need programmatic check for validation. 注意:如果您需要通过程序检查进行验证。 i would recommend you to do validation on form element. 我建议您对表单元素进行验证。 using 使用

<form name="valueForm" ng-submit="valueForm.$valid && checkValues();" class="form-inline">
<div class="form-group">
    <input type="text" name="amount" ng-model="newChange.amount" ng-maxlength="15" class="form-control"/>
    <button type="submit" class="btn btn-default" >Go</button>
</div>
</form>

Since you are not using angular-ui-bootstrap, here is a way to do it using Bootstrap's Javascript. 由于您未使用angular-ui-bootstrap,因此这里是一种使用Bootstrap的Javascript进行操作的方法。

Since it's DOM related, it'd be best to put this in a directive like so. 由于它与DOM相关,因此最好将其放入类似的指令中。 This gives you a reusable button. 这为您提供了可重复使用的按钮。 All you need to do is pass it a function reference which it will call to check whether or not to open the modal. 您需要做的就是向它传递一个函数引用,它将调用该函数引用来检查是否打开模态。

HTML: HTML:

<div open-modal="#myModal" check="demo.check()">Go</div>

JS: JS:

.directive('openModal', function() {
  return {
    restrict: 'EA',
    scope: {},
    replace: true,
    bindToController: {
      check: '&',
      openModal: '@'
    },
    transclude: true,
    template: '<button type="button" class="btn btn-primary btn-lg" ng-transclude ng-click="vm.onClick()"></button>',
    controllerAs: 'vm',
    controller: function() {
      var vm = this;

      vm.onClick = function() {
        if (vm.check()) {
          $(vm.openModal).modal('show');
        }
      };
    }
  }
});

Plunker: http://plnkr.co/edit/ukDuPZOaGrYQSwCnhe51?p=preview 柱塞: http ://plnkr.co/edit/ukDuPZOaGrYQSwCnhe51?p=preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM