我有一个按钮:
<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>
我还有一个 Angular 函数:
$scope.deleteCompany = function(id){
console.log(id);
}
当我在确认中单击“取消”时,控制台输出:
1
当我在确认中单击“确定”时,控制台输出:
1
我希望在按下“确定”时执行 Angular 代码,而不是在按下“取消”时执行。
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/
关于javascript - 使用确认从按钮中的 JavaScript 停止 Angular ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37028105/