2017-03-08 4 views
0

主な範囲では、ng-repeatが実行されています。指令AngularJsからコントローラ機能にアクセスするにはどうすればよいですか?

<table class="datatable" prevent-right-click visible="isVisible"> 
       <tr ng-repeat="t in templateData"> 
        <td confusedFunction="clickedonname(t)">{{t.templateName}}</td> 
        <td>{{t.templatePath}}</td> 
       </tr> 
      </table> 

防ぐ-右クリックして、最初のtd要素に、それぞれ右クリック要素にコメントを取ることでコメントボックスを持つカスタムコンテキストメニューです。とにかく、繰り返し要素を受け取り、その要素にコメントを記録できるように指示を渡す関数を書くことができますか?また、防止右クリックには独立したスコープがあります。

これは私の指示コードです。ここで

app.directive('preventRightClick', function() { 
    return { 
     restrict: 'A', 
     scope: { 
      visible: '=' 
     }, 
     link: function($scope, $ele) { 
      $ele.on('contextmenu', '*', function(e) { 
       e.preventDefault(); 
       $scope.$apply(function() { 
        $scope.visible = true; 
        $('.parented').css({right:50, top:50}).show(); 
       }) 
       e.stopPropagation(); 
      }); 

      $(document).on('click', '*', function (e) { 
       if ($(e.target).parents('.parented').length > 0) { 
       } 
       else{ 
        $('.parented').hide() 
        $ele.off('contextmenu', '*', function(){ 
         console.log('Context menu off') 
        }) 
       } 
      }) 
      $scope.confusedFunction = function(t){ 
       console.log(t.templateName) 
       console.log('coming from clickedonname') 
      } 
     } 
    }; 
}) 
+0

あなたはクリックを受信​​が情報を送信するようにしたいですrightRightClickディレクティブを防ぐには? –

+0

はい、引数として(t)を渡す必要があります。私は指示の中に繰り返しオブジェクトが必要です。 @JulienTASSIN – Aijaz

+0

もしそうなら、1つ目の方法はコントローラ機能のイベントを$ rootScopeに対応する$を指示文で$ブロードキャストすることです。 –

答えて

0

は、第二の方法について簡単な例です:

HTML:

<table class="datatable" prevent-right-click visible="isVisible" foo=current.selected ng-init="current = {}"> 
    <tr ng-repeat="t in templateData"> 
    <td ng-click="current.selected = t">{{t.templateName}}</td> 
    <td>{{t.templatePath}}</td> 
    </tr> 
</table> 

指令JS:

app.directive('preventRightClick', function() { 
    return { 
     restrict : "A", 
     scope: { 
      foo: '=foo', 
     }, 
     link : function (scope, element, attrs) { 
      scope.$watch('foo', function(n) { 
      // This code will be triggered with the new t in new value on each td click 
      }) 

    } 
    }; 
}); 
+0

ねえ、ありがとう。しかし、どのように関数にng-repeatの値を渡すのですか?私はそれが必要です。 – Aijaz

+0

こんにちは、tはng-repeatからdirectiveにcurrent.selectedで渡されます。ウォッチ関数でそれを変更し、そのディレクティブを使用することができます。 –

関連する問題