使用confirm从按钮中的javascript停止angular

Stop angular from javascript in button using confirm

本文关键字:javascript 停止 angular confirm 按钮 使用      更新时间:2023-09-26

我有一个按钮:

<button ng-click="deleteCompany(company.id)" 
  class="btn btn-danger" 
  onClick="return window.confirm('This will permernently delete the entity'n
           Are you sure you want to delete this post?'); ">
   <span class="glyphicon glyphicon-trash"></span>
</button>

我还有一个角度函数:

$scope.deleteCompany = function(id){
        console.log(id);
}

当我在确认中单击取消时,控制台输出:

1

当我在确认控制台输出中点击ok时:

1

我希望在按下"确定"时执行角度代码,但在按下"取消"时不执行。

JSFiddle示例:http://jsfiddle.net/4304ewu5/

confirm放入$scope.deleteCompany()函数:

function MyCtrl($scope) {
  $scope.deleteCompany = function(id) {
    if (confirm('This will permernently delete the entity'nAre you sure you want to delete this post?')) {
      $scope.name = id;
      // delete the stuff.
    }
  }
}

并将其从内联HTML:中删除

<div ng-controller="MyCtrl">
  <button ng-click="deleteCompany('1')" class="btn btn-danger">
    <span class="glyphicon glyphicon-trash"></span>
  </button>
  <button ng-click="deleteCompany('2')" class="btn btn-danger">
    <span class="glyphicon glyphicon-trash"></span>
  </button>
  Hello, {{name}}!
</div>

Fiddle:http://jsfiddle.net/0Laztrfk/

为什么不将confirm放在deleteCompany方法中?

$scope.deleteCompany = function(id) { 
    if (!confirm("Delete company?"))  
        return;
    console.log(id);
}