2015-04-28 79 views
0

我結束了這段代碼:確認指令不起作用

MetronicApp.directive('confirmClick', ['SweetAlert', 
function(SweetAlert) { 
    return { 
     priority: 100, 
     restrict: 'A', 
     scope: { 
      confirmClick: '&' 
     }, 
     link: { 
      pre: function(scope, element, attr) { 
       var msg = attr.ngConfirmClick || "Are you sure?"; 
       var clickAction = attr.confirmedClick; 
       element.bind('click touchstart', function(event) { 
        SweetAlert.swal({ 
          title: "Are you sure?", 
          text: "Your will not be able to recover this imaginary file!", 
          type: "warning", 
          showCancelButton: true, 
          confirmButtonColor: "#DD6B55", 
          confirmButtonText: "Yes, delete it!", 
          cancelButtonText: "No, cancel plx!", 
          closeOnConfirm: false, 
          closeOnCancel: true 
         }, 
         function(isConfirm) { 
          if (isConfirm) { 
           scope.confirmClick(); 
          } 
          else{ 
           return false; 
          } 
         }); 
       }); 
      } 
     } 
    }; 
} 
]); 
+0

您收到哪個錯誤? – manzapanza

+0

附註:切勿在您的指令名稱中使用'ng',這些應該是僅由角度創建的指令。讓其他開發者感到困惑!爲你/你的公司定製一個讓我們說'myConfirmClick' –

+0

@manzapanza我沒有得到任何錯誤。但它不像一個確認。 – Burak

回答

1

ng-click="delete(theme)"會一直陪在你點擊

要做的事情就是你的函數傳遞給你的指令被觸發:

MetronicApp.directive('ngConfirmClick', ['SweetAlert', 
function(SweetAlert) { 
return { 
    priority: 100, 
    restrict: 'A', 
    scope: { 
     ngConfirmClick : '&' 
    }, 
    link: { 
     pre: function(scope, element, attr) { 
      var msg = attr.ngConfirmClick || "Are you sure?"; 
      var clickAction = attr.confirmedClick; 
      element.bind('click touchstart', function(event) { 

       SweetAlert.swal({ 
         title: "Are you sure?", 
         text: "Your will not be able to recover this imaginary file!", 
         type: "warning", 
         showCancelButton: true, 
         confirmButtonColor: "#DD6B55", 
         confirmButtonText: "Yes, delete it!", 
         cancelButtonText: "No, cancel plx!", 
         closeOnConfirm: false, 
         closeOnCancel: false 
        }, 
        function(isConfirm) { 
         if (isConfirm) { 
          ngConfirmClick(); 
          SweetAlert.swal("Deleted!", "Your imaginary file has been deleted.", "success"); 
         } else { 
          SweetAlert.swal("Cancelled", "Your imaginary file is safe :)", "error"); 
         } 
        }); 
      }); 
     } 
    } 
}; 

}

,並使用指令,方式:

<a ng-confirm-click="delete(theme)" class="btn btn-danger btn-md ng-scope" >Delete</a> 
+0

謝謝@Deblaton Jean-Philippe。我將你的答案標記爲我的問題的答案。 – Burak

-3

我看到你錯過支架[「SweetAlert」添加支架,然後再試一次 增加它的功能到底。

MetronicApp.directive('ngConfirmClick', ['SweetAlert', 
function(SweetAlert) { 
return { 
    priority: 100, 
    restrict: 'A', 
    link: { 
     pre: function(scope, element, attr) { 
      var msg = attr.ngConfirmClick || "Are you sure?"; 
      var clickAction = attr.confirmedClick; 
      element.bind('click touchstart', function(event) { 

       SweetAlert.swal({ 
         title: "Are you sure?", 
         text: "Your will not be able to recover this imaginary file!", 
         type: "warning", 
         showCancelButton: true, 
         confirmButtonColor: "#DD6B55", 
         confirmButtonText: "Yes, delete it!", 
         cancelButtonText: "No, cancel plx!", 
         closeOnConfirm: false, 
         closeOnCancel: false 
        }, 
        function(isConfirm) { 
         if (isConfirm) { 
          SweetAlert.swal("Deleted!", "Your imaginary file has been deleted.", "success"); 
         } else { 
          SweetAlert.swal("Cancelled", "Your imaginary file is safe :)", "error"); 
         } 
        }); 
      }); 
     } 
    } 
};}]) 
+1

我應該在哪裏添加它?我認爲它已經被添加了。 – Burak