如何在toastr中显示确认对话框

时间:2016-06-08 11:51:43

标签: javascript angularjs toastr

我在控制器中有一个删除按钮的代码,

$scope.removes = function (scope) {
        toastr.success("Delete all?","<br /><br /><button type='button' class='btn clear'>Yes</button>",{
            closeButton: false,
            onClick: function(){
                var nodeData = scope.$modelValue;
                            if(nodeData.nodes.length > 0){
                                toastr.error('Cant delete Sub levels available :', 'Warning', {
                                    closeButton: true
                                });
                            }else{
                                mainMenuService.deleteData(nodeData).success(function(data) {
                                    scope.remove();
                                    toastr.success(data.message, 'Message', {
                                        closeButton: true
                                    });
                                }).error(function(err) {
                                    toastr.error(err, 'Warning', {
                                        closeButton: true
                                    });
                                });
                            }
            }
        })
}

我想显示一个确认对话框,如果使用单击是按钮,则要删除。但我在toastr消息中看不到任何按钮,我不知道该怎么做。我完全按照文档中的说法完成了。我想知道是否可以在确认消息中放两个按钮?

6 个答案:

答案 0 :(得分:8)

如果有人不是在Angular解决方案之后,而是回到基础知识,那么它非常简单。

toastr.success("<br /><br /><button type='button' id='confirmationRevertYes' class='btn clear'>Yes</button>",'delete item?',
  {
      closeButton: false,
      allowHtml: true,
      onShown: function (toast) {
          $("#confirmationRevertYes").click(function(){
            console.log('clicked yes');
          });
        }
  });

答案 1 :(得分:2)

首先,你需要为toastr设置allowHtml: true选项,如下所示:

$scope.removes = function (scope) {
    toastr.success("<br /><br /><button type='button' class='btn clear'>Yes</button>",'delete item?',
    {
        closeButton: false,
        allowHtml: true,
        ...
    })
}

也是toastr Title是第二个参数,内容是第一个。

我假设您使用的是Angular Toastr,如果是这样,首先您需要知道它没有任何onClick事件。onTap是您点击时触发的事件toastr。但是在你点击toastr上的任何地方后它会触发:

toastr.success("Content",'Title',
{
    onTap: function(){
       //Triggers when you click any where on toastr
    },
    ...
});

所以我认为你想要一个在点击按钮时触发的事件,在这种情况下,只要Angular Toastr因为安全原因不能接受内容或标题部分中的指令,你需要将click事件附加到您的按钮位于toastr的onShow事件中,如下所示:

$scope.yes = function() {
   alert("You Clicked Yes!!!");
}
var html = "<br /><br /><button type='button' class='btn clear'>Yes</button>";
toastr.success(html,'Are You Sure?',
{
  allowHtml: true,
  onShown: function (toast) {
    angular.element( toast.el[0]).find("button")[0].onclick = $scope.yes;

  }
});

我在 Plunker

汇总了一个样本

希望这会有所帮助

答案 2 :(得分:0)

toastr.warning("<br /><button type='button' value='yes'>Yes</button><button type='button'  value='no' >No</button>",'Are you sure you want to delete this item?',
{
    allowHtml: true,
    onclick: function (toast) {
      value = toast.target.value
      if (value == 'yes') {
        console.log(toast.target.value, 'carai')  
      }
    }

})

答案 3 :(得分:0)

在angularjs中使用烤面包机的确认弹出窗口 在这里,您>> >> http://plnkr.co/edit/Ri2ELEglunzesYkO

toastr.success(html,'Are You Sure?',
{
  allowHtml: true,
  timeOut: 50000,
  tapToDismiss: false,
  extendedTimeOut: 100000,
  onShown: function (toast) {
    angular.element( toast.el[0]).find("button")[0].onclick = $scope.yes;
    angular.element( toast.el[1]).find("button")[1].onclick = $scope.no;
  },
  onTap:function(){
    alert(($scope.yesno==true)? 'Yes':'No');
  }
});

答案 4 :(得分:-1)

如果你想添加确认框。最好使用alertify。它易于使用和理解。请参阅以下链接:

http://fabien-d.github.io/alertify.js/

答案 5 :(得分:-2)

为什么不

toastr.confirm('Are you sure?', {onOk: () => { console.log('ok') }, onCancel: () => { console.log('cancel')}})

enter image description here

相关问题