【问题标题】:Confirm dialog box in angularjsangularjs中的确认对话框
【发布时间】:2016-05-31 18:57:33
【问题描述】:

如何在 angularjs 的下方按钮中应用确认对话框?

<button class="btn btn-sm btn-danger" ng-click="removeUser($index)">Delete</button>

就这样。

<span><a class="button" onclick="return confirm('Are you sure to delete this record ?')" href="delete/{{ item.id }}">Delete</span>

更新

目前我正在这样做

    function removeUser(index) {
      var isConfirmed = confirm("Are you sure to delete this record ?");
      if(isConfirmed){
        vm.users.splice(index, 1);
      }else{
        return false;
      }
    };

【问题讨论】:

    标签: javascript angularjs confirm-dialog


    【解决方案1】:
    $scope.removeUser= function (ind){
     if (confirm("Are you sure?")) {
        alert("deleted"+ s);
        $window.location.href = 'delete/'+ s;
     }
    }
    

    http://jsfiddle.net/ms403Ly8/61/

    【讨论】:

    • 完美答案!!
    【解决方案2】:

    我会将消息位与删除操作位分开,这样您就可以在应用程序的其他部分重用确认位: 我使用这样的指令:

    angular.module('myModule').directive("ngConfirmClick", [
      function() {
       return {
         priority: -1,
          restrict: "A",
          link: function(scope, element, attrs) {
            element.bind("click", function(e) {
              var message;
              message = attrs.ngConfirmClick;
              if (message && !confirm(message)) {
               e.stopImmediatePropagation();
               e.preventDefault();
              }
            });
          }
        };
      }
    ]);
    

    然后让您的控制器功能与删除操作:

    $scope.removeUser(index) {
      //do stuff
    }
    

    在视图中我会使用 ng-click:

    <span><a class="button" ng-confirm-click="Are you sure?" ng-click="removeUser(item.id}}">Delete</span>
    

    希望对你有帮助。

    【讨论】:

      【解决方案3】:

      你可以试试这个插件:http://plnkr.co/edit/xJJFxjYeeHmDixAYPu4c?p=preview 您可以为对话框创建指令。

      var app = angular.module('plunker', []);
      
       app.controller('MainCtrl', function($scope, $window) {
      
         $scope.delete = function(id) {
           $window.location.href = 'delete/'+ id;
         }
       });
      
       app.directive('ngConfirmClick', [
          function(){
              return {
                  link: function (scope, element, attr) {
                      var msg = attr.ngConfirmClick || "Are you sure?";
                      var clickAction = attr.confirmedClick;
                      element.bind('click',function (event) {
                          if ( window.confirm(msg) ) {
                              scope.$eval(clickAction)
                          }
                      });
                  }
              };
      }])
      

      【讨论】:

        【解决方案4】:

        这里是sn-ps,

        你的 HTML 应该是怎样的,

        <button class="btn btn-sm btn-danger" ng-confirm-click="Are you sure to delete this record ?" confirmed-click="removeUser($index)">Delete</button>
        

        请将此指令包含在您的自定义 angularjs 文件中,

        app.directive('ngConfirmClick', [
            function(){
                return {
                    link: function (scope, element, attr) {
                        var msg = attr.ngConfirmClick || "Are you sure?";
                        var clickAction = attr.confirmedClick;
                        element.bind('click',function (event) {
                            if ( window.confirm(msg) ) {
                                scope.$eval(clickAction)
                            }
                        });
                    }
                };
        }])
        

        您基于上述删除功能的角度范围,

        $scope.removeUser = function(index) {
            vm.users.splice(index, 1);
        }
        

        【讨论】:

        • 绑定已被弃用。改用 on。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-27
        • 2011-05-24
        • 2016-02-02
        • 2023-04-03
        相关资源
        最近更新 更多