【问题标题】:How to show alert on tags remove using angularJs?如何在使用 angularJs 删除标签时显示警报?
【发布时间】:2017-04-24 04:39:18
【问题描述】:

我正在使用 ng-tags-input 库来显示标签,但我想在用户删除标签时显示一个 sweetAlert。我尝试了这个,但我注意到在屏幕上显示警报之前删除了标签。请参阅下面的屏幕截图。

从第二个屏幕截图中您可以看到,当我删除标签 July-2017 时,它将在确认警报框之前删除 July-2017 标签。在这种情况下,没有办法使用警告框。

在编码级别我是这样做的

HTML

<tags-input ng-model="monthTags" display-property="text" placeholder="{{ ''}}" add-from-autocomplete-only="true"  on-tag-removed="removeDiv($tag)" add-on-paste="true" on-tag-added="addDiv($tag)"  ng-required="true">
    <auto-complete  min-length="1" source="loadTags($query)"> </auto-complete>
</tags-input>

JS

$scope.removeDiv = function (tag) {

    if (tag.id) {
        swal({
            title: "Are you sure?",
            text: "You want to remove this.",
            showCancelButton: true,
            type: "warning",
            confirmButtonColor: "#37BC9B",
            confirmButtonText: "Yes, delete it!",
            closeOnConfirm: true,
        },
        function () {
            //Ajax request
        });
    }
}

现在我需要做什么来实现这一目标。请帮忙

【问题讨论】:

    标签: angularjs alert ng-tags-input


    【解决方案1】:

    您可以使用 ngTagsInput 的 onTagRemoving 回调。这是in the documentation。它指出

    在删除标签之前将调用的评估表达式。这 标签可用作 $tag。此方法必须返回 布尔值 价值或承诺。如果错误值或被拒绝的承诺是 返回,标签不会被移除。

    您需要将 on-tag-removed 更改为 on-tag-removing 并返回布尔值 true 以删除或 false 以恢复。

    【讨论】:

    • 你的答案是正确的,但是应该返回一个promise而不是一个布尔值,因为它是一个异步操作(它依赖于用户做某事)。
    • @MichaelBenford 根据官方文档,它可以从 Promise 和 boolean 返回任一值。我也试过小提琴。返回布尔值
    • 我知道文档的内容,主要是因为我是编写文档的人。 :) 返回布尔值仅适用于同步操作,但 OP 描述了异步场景(屏幕上的对话框等待用户决定要做什么)。对于这种情况,必须返回一个承诺。
    【解决方案2】:

    您需要使用promise,因为这里屏幕上有一个对话框等待用户决定要做什么。我试着用你的代码给你一些提示,比如:

    HTML

    <tags-input ng-model="monthTags" display-property="text" placeholder="Add Months" add-from-autocomplete-only="true"  on-tag-removed="removeDiv($tag)" add-on-paste="true" on-tag-added="addDiv($tag)"  ng-required="true">
        <auto-complete  min-length="1" source="loadTags($query)"> </auto-complete>
    </tags-input>
    

    JS

    $scope.removeDiv = function (tag) {
        let promiseDivRemove = new Promise(function(resolve, reject) {
            if (tag.id) {
                swal({
                    title: "Are you sure?",
                    text: "You want to remove this.",
                    showCancelButton: true,
                    type: "warning",
                    confirmButtonColor: "#37BC9B",
                    confirmButtonText: "Yes, delete it!",
                    closeOnConfirm: true,
                },
                function (isConfirm) {
                    if (isConfirm) {
                        //Ajax request
                        resolve(true);    
                    } else { reject(false);}
                });
            }else{ reject(false);}
        }).then(
            function(result) { return result; },
            function(error) { return error; }
        );;
        return promiseDivRemove;
    }
    

    【讨论】:

      【解决方案3】:

      您应该在请求的成功函数中显示警报。

      $http.get(url).remove().then(function (response) {
         showAlert();
      })
      .error(function(error){
      
      })
      

      【讨论】:

      • @Ramkishan 结果是什么?
      • 不工作,因为标签在屏幕上显示警告框之前被删除
      【解决方案4】:

      您需要像这样检查用户的确认是真还是假:

      $scope.removeDiv = function (tag) {
        if (tag.id) {
            swal({
              title: "Are you sure?",`enter code here`
              text: "You want to remove this.",
              showCancelButton: true,
              type: "warning",
              confirmButtonColor: "#37BC9B",
              confirmButtonText: "Yes, delete it!",
              closeOnConfirm: true,
          },
          function (isConfirm) {
                   if(isConfirm){
                          Ajax request
                    }
          });
        }
      }
      

      【讨论】:

      • 标签已在屏幕@jenny 显示警告框之前删除。
      • 很遗憾你的回答没有解决我的问题:(
      • @Ramkishan 请将 on-tag-removing="removeDiv($tag)" 替换为 html 标签输入标签中的 on-tag-removed="removeDiv($tag)"。跨度>
      • 我也按照@Agam Banga 的建议尝试过,但这对我也不起作用。
      • @Ramkishan 你能创建 plunker 吗?
      【解决方案5】:
      <tags-input ng-model="tags" on-tag-removing="validateRemoval($tag)"></tags-input>
      
        $scope.validateRemoval = function(tag) {
           alert('Are you sure you want to delete?');
        }
      

      【讨论】:

      • 此答案已标记为待审核 (NAA),请edit 并包含有关代码的正确描述 - 我将其审核为“看起来不错”,因为代码本身是正确的,但仍需要解释未来有同样问题的用户
      猜你喜欢
      • 2019-07-14
      • 2019-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多