2017-04-08 59 views
0

您能否解釋爲什麼我的ng-confirm-click不起作用?Ang-ng-confirm-click不起作用

<div ng-controller="MainCtrl" ng-init="show=true;"> 
    <p >Hello {{name}}!</p> 
    <button ng-show="show" confirmed-click="changes();show=false;" ng-confirm-click="Confirm change?">change</button> 
</div> 

我需要隱藏按鈕。

在這裏,例如:

<!DOCTYPE html> 
<html> 

    <head> 
    <meta charset="utf-8" /> 
    <title>AngularJS Plunker</title> 
    <script>document.write('<base href="' + document.location + '" />');</script> 
    <link rel="stylesheet" href="style.css" /> 
    <script data-require="[email protected]" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script> 
    <script src="app.js"></script> 
    </head> 

    <body ng-app = "plunker"> 
    <div ng-controller="MainCtrl" ng-init="show=true;"> 
     <p >Hello {{name}}!</p> 
     <button ng-show="show" confirmed-click="changes();show=false;" ng-confirm-click="Confirm change?">change</button> 
    </div> 
    </body> 

</html> 
+0

我編輯了它的工作原理 – aramadhani

回答

0

scope.$apply使用代替scope.$eval和從控制器刪除不必要scope.$apply

從技術文檔:

這種使用$的eval:範圍$的eval()似乎贊成的範圍被棄用$適用於執行使用和修改的範圍的功能,並更新。稍後查看。

Plunker

2

試試這個工作代碼,

裏面的html:

<div ng-confirm-click="Are you sure to delete this product ?" confirmed-click="deletePost(data.postId)"> <span class="glyphicon glyphicon-trash"></span></div> 

在app.js文件,把下面的代碼(沒有任何變化):

app.directive('ngConfirmClick', [ 
    function(){ 
     return { 
      link: function (scope, element, attr) { 
       var msg = attr.ngConfirmClick || "Are you sure?"; 
       var clickAction = attr.confirmedClick; 
       element.bind('click',function (event) { 
        if (window.confirm(msg)) { 
         scope.$eval(clickAction) 
        } 
       }); 
      } 
     }; 
}])