2016-05-04 15 views
0

を確認して使用して、ボタン内のJavaScriptからストップ角度:私がボタンを持っている

<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 

「OK」を押したときに角度コードを実行し、「キャンセル」を押したときには角度コードを実行します。

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

答えて

5

あなたconfirm$scope.deleteCompany()関数内を投げ

<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> 

フィドル:http://jsfiddle.net/0Laztrfk/

3

は、なぜあなたはあなたのdeleteCompanyメソッド内confirmを入れないのですか?

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から削除:

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

    console.log(id); 
} 
関連する問題