简体   繁体   English

仅在输入所有输入字段时关闭模式

[英]close modal only if all input fields are entered

I have this plunker to show my issue. 我有这个笨拙的人来展示我的问题。

Once user clicks on "Open Modal" a modal opens up showing input fields. 用户单击“打开模式”后,将打开一个模式,显示输入字段。 If user enters on all the 3 input fields, it should close the modal. 如果用户在所有3个输入字段上输入,则应关闭模式。 This works fine. 这很好。

Now, if user forgets to mention any one of the field, it gives a alert message showing us to enter values to the fields... After this message, the modal should remain open. 现在,如果用户忘记提及该字段中的任何一个,它会显示一条警告消息,指示我们向这些字段输入值...在此消息之后,模态应该保持打开状态。 in my case, it closes after showing the alert. 就我而言,它在显示警报后关闭。

I tried to remove the hide function from here 我试图从这里删除隐藏功能

ng-click="$hide();adduser()"

So instead of the above, i tried this 因此,除了上面的,我尝试了这个

ng-click="adduser()"

This solves the problem. 这样就解决了问题。 ie it gives an alert when one of the fields are missing. 即,当其中一个字段丢失时,它会发出警报。 But the other issue comes up which was working in the first scenario. 但是出现了另一个问题,该问题在第一种情况下有效。 after user enters all the 3 values and clicks 'add' the modal doesnt close. 用户输入所有3个值并单击“添加”后,模态不会关闭。 since i have removed hide() functionality from ng-click. 因为我已经从ng-click中删除了hide()功能。

Can someone tell me how to get both the cases possible and working. 有人可以告诉我如何使案件同时可行。

You can simply add validations to your form and then you can disable the button to prevent user click on button without fill all fields, like this: 您可以简单地向表单添加验证,然后可以禁用按钮以防止用户单击按钮而不填写所有字段,例如:

ng-disabled="form.$invalid"

So you can have something like this in your modal: 因此,您可以在模态中包含以下内容:

<div class="modal ng-scope center am-fade-and-scale" tabindex="-1" role="dialog" aria-hidden="true" style="z-index: 1050; display: block;">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header" ng-show="title">
        <button type="button" class="close" aria-label="Close" ng-click="$hide()"><span aria-hidden="true">×</span></button>
        <h4 class="modal-title" ng-bind-html="title"></h4></div>
      <div class="modal-body">
        <form novalidate name="form">
          <div class="form-group">
            Select Sedol:
            <input name="select1" type="text" ng-model="selectedState" bs-options="state for state in states" placeholder="Enter state" ng-change="get_change(selectedState)" bs-typeahead required>
          </div>
          <div class="form-group">
            RestrName:&nbsp;&nbsp;
            <select name="select2" id="restrName" ng-model="selectedOption" ng-change="set_value(selectedOption)" required>
              <option value="sedol">sedol</option>
              <option value="cusip">cusip</option>
            </select>
          </div>

          <div class="form-group">
            RestrType:&nbsp;&nbsp;
            <select name="select3" id="restrType" ng-model="selectedOption1" ng-change="set_value1(selectedOption1)" required>
              <option value="NoBuy">NoBuy</option>
              <option value="NoSell">NoSell</option>
              <option value="NoHold">NoHold</option>
              <option value="NoTrade">NoTrade</option>
              <option value="NoincExp">NoincExp</option>
              <option value="NoDecExp">NoDecExp</option>
              <option value="NoHoldLong">NoHoldLong</option>
              <option value="NoHoldShort">NoHoldShort</option>
              <option value="NoCoverBuy">NoCoverBuy</option>
              <option value="NoSellShort">NoSellShort</option>
            </select>
          </div>
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" ng-disabled="form.$invalid" ng-click="$hide();adduser()">Add</button>
      </div>
    </div>
  </div>
</div>

DEMO 演示

Well, if you want to hide your modal, something has to trigger it. 好吧,如果您想隐藏模态,则必须触发某些事情。 I'm not sure how to do it properly from your controller right now, you could use jquery to hide it from inside addUser() method. 我不确定现在如何从您的控制器正确执行此操作,您可以使用jquery从addUser()方法内部将其隐藏。

A cleaner solution imo is to use https://angular-ui.github.io/bootstrap/#/modal then you can programmatically close it (and more) in a nicer way. 较干净的imo解决方案是使用https://angular-ui.github.io/bootstrap/#/modal,然后您可以通过编程方式以一种更好的方式关闭它(以及更多)。

You should be using your controller to manage the lifecycle of the modal. 您应该使用控制器来管理模态的生命周期。 I have created a new plunker that shows you how to do this: 我创建了一个新的插件,向您展示如何执行此操作:

http://plnkr.co/edit/k0yDjAcUbRhUtm1vQ2Ck?p=preview http://plnkr.co/edit/k0yDjAcUbRhUtm1vQ2Ck?p=preview

  $scope.detailsModal = $modal({ 
    scope: $scope,
    title: 'Enter details', 
    html: true, 
    show: false,
    templateUrl: 'modal/docs/modal.demo.tpl.html'
  });

  $scope.showModal = function() {
    $scope.detailsModal.show();
  }

  $scope.adduser = function() {
    if ($scope.selectedState && $scope.selectedOption && $scope.selectedOption1) {
      $scope.detailsModal.hide();
    }
    else {
      window.alert('please select all required fields');
    }
 }

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

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