【问题标题】:Navigate after click on ok button of confirm box using angular.js使用 angular.js 单击确认框的确定按钮后导航
【发布时间】:2015-10-29 19:51:01
【问题描述】:

我有一个问题。我的要求是当用户尝试移动到下一个状态/页面时,会出现一个确认框,如果用户单击确定按钮,那么只有它会导航到下一个状态,否则它将保持原样.我在下面解释我的代码。

<li ui-sref-active="active" ><a ui-sref=".profile" confirm="Are you sure to move into next page?">Profile</a></li>
<li ui-sref-active="active"><a ui-sref=".dept" >Department</a></li>

假设用户点击配置文件菜单,第一个确认框会出现,然后按下确定它会移动。

 dashboard.directive('confirm', function(ConfirmService, $state) {
    return {
        restrict: 'A',
        scope: {
            eventHandler: '&ngClick'
        },
        link: function(scope, element, attrs){
          element.unbind("click");

          element.bind("click", function(e) {
            e.preventDefault(); // block the href generated by ui-sref
            ConfirmService.open(attrs.confirm, $state.go(attrs.uiSref)); // pass ui-sref into $state.go
          });
        }
    }
});

    dashboard.service('ConfirmService', function($uibModal) {
      var service = {};
      service.open = function (text, onOk) {
        var modalInstance = $uibModal.open({
          templateUrl: 'myModalContent.html',
          controller: 'ModalConfirmCtrl',
          resolve: {
            text: function () {
              return text;
            }
          }
        });

        modalInstance.result.then(function (selectedItem) {
          onOk();
        }, function () {
        });
      };

      return service;
    })

    dashboard.controller('ModalConfirmCtrl', function ($scope, $uibModalInstance, text) {

      $scope.text = text;

      $scope.ok = function () {
        $uibModalInstance.close(true);
      };

      $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
      };
    });

更新的 js 代码

dashboard.directive('confirm', function($uibModal, $state,$timeout) {
    return {
        restrict: 'A',
        scope: {
            eventHandler: '&ngClick'
        },
        link: function(scope, element, attrs){
        var isConfirmed = false;

        element.bind("click", function (e) {
            if (!isConfirmed) {
                e.preventDefault();
                $uibModal.open({
                    templateUrl: 'myModalContent.html',
                    controller: function ($scope, $uibModalInstance) {
                        //$scope.text=text;
                        $scope.ok = function () { $uibModalInstance.close(); }
                        $scope.cancel = function () { $uibModalInstance.dismiss(); }
                    }
                })
                .result.then(function () {
                    isConfirmed = true;
                    $timeout(function () {
                        element.click();
                    });
                });
            }
        });
    }
    }
});

这里也出现了确认框,但问题是它首先移动到配置文件状态并显示确认框。我需要在移动到下一个状态之前确认框应该显示,按下确定后它将导航。我试过了在那里使用ng-click,但使用它会丢失菜单上的active 类和cursor:pointer 属性。请帮我解决这个问题。

【问题讨论】:

  • ui-sref 传递到OK button 上的下一个视图,然后单击Cancel button 关闭模式。那应该可以..我猜..
  • @MoidMohd:你能编辑你的答案吗?
  • 在您的模态模板中将ui-sref 属性添加到OK 按钮以转到其他状态(您想要点击ok 按钮的状态)和“取消”按钮只需关闭模态。
  • @MoidMohd :我可以理解,但我怎样才能动态传递所需的状态。我有点困惑。你能根据我的代码举一个例子吗。

标签: javascript angularjs angular-ui-router angular-ui-bootstrap


【解决方案1】:

更新:解析ui-sref 以用于$state.go 比我想象的要困难。最后我选择再次人工“点击”元素...

    link: function(scope, element, attrs){
        var isConfirmed = false;

        element.bind("click", function (e) {
            if (!isConfirmed) {
                e.preventDefault();
                $modal.open({
                    template: '<div ng-click="close()">yes</div><div ng-click="dismiss()">no</div>',
                    controller: function ($scope, $modalInstance) {
                        $scope.close = function () { $modalInstance.close(); }
                        $scope.dismiss = function () { $modalInstance.dismiss(); }
                    }
                })
                .result.then(function () {
                    isConfirmed = true;
                    $timeout(function () {
                        element.click();
                    });
                });

                // check if these 2 lines making any difference?
                e.stopImmediatePropagation();
                return false;
            }
            else {
                isConfirmed = false; // switch back to popup next time
            }
        });
    }

此部分已过时

你可以这样尝试(未经测试)

dashboard.directive('confirm', function(ConfirmService, $state) {
    return {
        restrict: 'A',
        scope: {
            eventHandler: '&ngClick'
        },
        link: function(scope, element, attrs){
          element.unbind("click");

          element.bind("click", function(e) {
            e.preventDefault(); // block the href generated by ui-sref
            ConfirmService.open(attrs.confirm, $state.go(attr.uiSref)); // pass ui-sref into $state.go
          });
        }
    }
});

注意:这仅用于概念验证。如果您有ng-click/ui-sref,您可能想要切换 if/else。

【讨论】:

  • @ lcycool :我添加了您的代码部分。在这里我也遇到了一些问题和错误。第一次当我点击个人资料菜单时,它正在移动到个人资料页面并显示确认框。第二次当我在部门页面并单击配置文件菜单时,它会抛出此Uncaught Error: Could not resolve '.profile' from state 'dashboard.dept' 错误。请检查我在帖子上更新的代码。
  • 您可以尝试在ui-sref 中传递完整路线'dashboard.dept''dashboard.profile' 吗?
  • @lcycool :我在两者中都添加了但仍然存在错误,并且在移动到页面后确认框即将到来。
  • @ lcycool :我尝试了您更新的代码。但是单击配置文件菜单后,问题是相同的,配置文件页面即将到来,然后确认框即将到来。按取消很好,但是当我单击时确定按钮,下次单击配置文件菜单没有确认框出现。这里还有我如何获得确认文本。我已经发布了 mu 更新的 js 代码。请检查。
  • @subhra 已更新。在我的测试代码中 preventDefault 可以停止状态转换,我不确定你的情况会发生什么。无论如何,让我们尝试添加一些其他代码,看看它们是否可以停止转换。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多