简体   繁体   English

AngularJS中的确认模态

[英]Confirmation Modal in AngularJS

I have an app that manages subscriptions. 我有一个管理订阅的应用程序。

<table>
  <thead>
    <tr>
      <th>Subscription Name</th>
      <th>Auto Renew</th>
    </tr>
  </thead>
  <tbody ng-controller="SubscriptionController as subCtrl>
    <tr ng-repeat="subscription in subCtrl.subscriptions">
      <td>
      {{ subscription.name }}
      </td>
      <td>
        <input type="checkbox" name="onoffswitch" class="autorenew onoffswitch-checkbox" id="switch-{{ subscription.id }}" value="1" ng-checked="subscription.auto_renew" switchtoggle="subCtrl.toggleAutoRenew({{subscription.id}})">
      </td>
    </tr>
   </tbody>
 </table>

The checkbox has a directive called switchToggle. 该复选框具有一个名为switchToggle的指令。 I use this to manage the model because I need to be able to watch for the change and ask for a confirmation Modal. 我用它来管理模型,因为我需要能够监视更改并要求确认模态。 The Directive just runs the toggleAutoRenew function of the controller 该指令仅运行控制器的toggleAutoRenew功能

/**
 *
 */
app.directive('switchtoggle', function () {
  return {
    restrict: 'A',
    link: function(scope, element, attrs)
    {
      element.bind('change', function(){ 
        scope.$apply(attrs.switchtoggle);
      });
    }
  };
});

What I haven't been able to figure out what to do is create a modal with confirm and cancel buttons. 我还没弄清楚该怎么做,就是用确认和取消按钮创建一个模态。 That way when someone clicks on the checkbox (which looks like an on off switch) I can confirm whether they really want it on or off. 这样,当有人单击复选框(看起来像一个关闭开关)时,我可以确认他们是否确实要打开或关闭它。

help please.... 请帮助....

So I'd like to also suggest using UI Bootstrap's Modal Dialog , but read the full answer before you string me up :) 因此,我也建议使用UI Bootstrap的“模态对话框” ,但在将我串起来之前先阅读完整的答案:)

First, here is a demo of your EXACT scenario . 首先,这是您的EXACT场景演示

Now let me walk through the code. 现在让我遍历代码。

The only change I made to your markup was to drop the custom directive in favor of a simple ng-click handler. 我对标记所做的唯一更改是放弃了自定义指令,转而使用了简单的ng-click处理程序。

ng-click="subCtrl.toggleAutoRenew($event, subscription)

Notice the $event I am passing in there, that is important because it allows us to be able to call .preventDefault() , which will stop the model from being updated, and give us a chance to show a confirmation dialog to the user. 请注意,我在其中传递了$event ,这一点很重要,因为它使我们能够调用.preventDefault() ,这将阻止更新模型,并使我们有机会向用户显示确认对话框。

The toggle method passes in the entire subscription object, and then simply cancels further event processing. toggle方法传入整个订阅对象,然后简单地取消进一步的事件处理。

toggleAutoRenew: function($event, subscription) {
      var _this = this;

      $event.preventDefault();
      $event.stopPropagation();

      _this.confirmAutoRenew(subscription)
        .then(function() {
          subscription.auto_renew = !subscription.auto_renew;
        });
    }

We then make a call to another method responsible for showing the dialog that returns a promise . 然后,我们调用另一个方法,该方法负责显示返回promise的对话框。

If the promise is resolved , then we toggle the auto_renew flag on the subscription object and everything works as expected. 如果promise被解决 ,那么我们切换subscription对象上的auto_renew标志,一切都会按预期进行。

If the promise is rejected , then we do nothing, and the subscription is never updated. 如果Promise被拒绝 ,那么我们什么也不做,并且订阅永远不会更新。 To the user, it's as though they never clicked the checkbox at all. 对于用户而言,就好像他们根本没有单击该复选框一样。

You can look at the example to see how that method is making use of UI Bootstrap and the $modal service to show a dialog and handle the result, but the logic is all inside the toggle method, and it's 100% handled by the controller . 您可以查看示例,以了解该方法如何利用UI Bootstrap和$modal服务显示对话框并处理结果,但是逻辑全在toggle方法内,并且100%由控制器处理

Here you can see how to open modals: http://angular-ui.github.io/bootstrap/ 在这里,您可以看到如何打开模态: http : //angular-ui.github.io/bootstrap/

Btw, here is how your input should look like: 顺便说一句,这是您输入的内容:

<input type="checkbox" class="autorenew onoffswitch-checkbox" ng-model="subscription.auto_renew" ng-change="subCtrl.toggleAutoRenew(subscription)">

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

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