2016-08-10 5 views
0

このディレクティブ:ディレクティブがエラーなしで失敗するのはなぜですか?

angular.module('WizmoApp', []) 
    .directive('confirmClick', function() { 
     return { 

       priority: -1, 
       restrict: 'A', 
       link: function(scope, element, attrs){ 
        element.bind('click', function(e){ 
         var message = attrs.ngConfirmClick; 
         // confirm() requires jQuery 
         if(message && !confirm(message)){ 
          //e.stopImmediatePropagation(); 
          //e.preventDefault(); 
         } 
        }); 
       } 
     }; 
    }); 

、私はここから転写しようとしている: Confirmation dialog on ng-click - AngularJS が急停止に私のアプリをもたらすのに十分です。 画面に何も表示されず、jsエラーも、何もない、空白の空白だけが表示されます。

これは、私はそれを使用している方法です:おろか、ディレクティブをデバッグする方法

<tr class="" 
    ng-repeat="package in adminManifestVm.Manifest | orderBy:'Id' track by $index" 
    ng-click="adminManifestVm.debinPackage(package.Id);" 
    ng-confirm-click="Are you sure you want to debin this?"> 

ないアイデアは、1を書きません。

[編集] この例では、指示は実際にはと呼ばれています。ngConfirmClick。変更しましたが、違いはありません。

答えて

1

あなたは解決策が機能していないことをスコープ

angular.module('WizmoApp', []) 
.directive('confirmClick', function() { 
    return { 
      scope:{ 
       val: '=' 
      }, 
      priority: -1, 
      restrict: 'A', 
      link: function(scope, element, attrs){ 
       element.bind('click', function(e){ 
        var message = val; 
        // confirm() requires jQuery 
        if(message && !confirm(message)){ 
         //e.stopImmediatePropagation(); 
         //e.preventDefault(); 
        } 
       }); 
      } 
    }; 
}); 
+0

ああ。正しいものとして吟味されていないスニペットをコピーするために私に奉仕します。おかげさまで – DaveC426913

+0

残念ながら、それはそのままでは機能しません。私はval '='または 'val is undefined'で 'unexpected string'を取得します。私はそれを試してみよう。 – DaveC426913

+0

今すぐ試してみてください –

0

を追加し、それが間違っ

<tr class="" 
ng-repeat="package in adminManifestVm.Manifest | orderBy:'Id' track by $index" 
ng-click="adminManifestVm.debinPackage(package.Id);" 
confirm-click val="Are you sure you want to debin this?"> 

とあなたのディレクティブでを使用しています。

Syntax Error: Token 'Package' is an unexpected token at column 6 of the expression [Move Package back to Pile?] starting at [Package back to Pile?].

これは私がそれを実現してきた方法です::このエラーは意味をなさない

指令:

(function(){ 
angular 
    .module('WizmoApp') 
    .directive('confirmClick', function() { 
     return { 
      priority: -1, 
      restrict: 'A', 
      link: function(scope, element, attrs){ 
       element.bind('click', function(e){ 
        var message = attrs.val; 
        // confirm() requires jQuery 
        if(message && !confirm(message)){ 
         e.stopImmediatePropagation(); 
         e.preventDefault(); 
        } 
       }); 
      } 
     } 
}); 
})(); 

ビュー:

<tr class="" 
    ng-repeat="package in adminManifestVm.Manifest | orderBy:'Id' track by $index" 
    ng-click="adminManifestVm.debinPackage(package.Id);" 
    confirm-click 
    val="Move Package back to Pile?"> 
関連する問題