2016-10-27 3 views
0

外の関数を呼び出す方法を、私はフォローを書く:cxControllerでHTMLに指示

<div ng-controller="cxController"> 
    <my-dir test-data="testdata"></my-dir> 
</div> 

、私がフォローを書く:ディレクティブで

$scope.testdata={ 
    spanInfo:{}, 
    buttonInfo:{} 
}; 
$scope.click1=function(){}; 
$scope.click2=function(){}; 
$scope.click3=function(){}; 

、いくつかのスパンとボタンがあります。ボタン番号が設定されていない場合は、ng-repeatを使用してそれを書き込む必要があります。しかし、ボタンにはクリック機能があります:click1、click2、click3など。ディレクティブの中でclick1、click2 ...という関数をどのように呼び出すことができますか?

この指示文には、buttonInfoにいくつかの機能がありますか?

$scope.testdata={ 
    spanInfo:{}, 
    buttonInfo:{ 
    {click:$scope.click1} 
    {click:$scope.click2} 
    } 
}; 
+0

ディレクティブは何 'scope'性質を持っているのですか?また、* "index" *引数を受け入れるメソッドを使うのはなぜですか? – Phil

答えて

0

これは私がディレクティブからコントローラ関数を呼び出す方法です。 これを利用できますか?

あなたのディレクティブ

(function() { 
    'use strict'; 
    angular 
     .module('app') 
     .directive('myDir', myDir); 

    myDir.$inject = []; 

    function myDir() { 
     return { 
      restrict: 'AE', 
      scope: { 
       onClick : '&' , 
      }, 
      template: '<div>'+ 
         '<button ng-click="onClick()"></button'+ 
        '</div>', 
      link: function(scope, element, attrs) { 

      } 
     } 
    } 
})(); 

コントローラ

(function() { 
    'use strict'; 

    angular 
     .module('app') 
     .controller('myController', myController); 

    myController.$inject = []; 

    function myController() { 

     var vm = this; 

     vm.callControllerFn = function(){ 
      alert("called") 
     } 

    } 
})(); 

ビュー

<my-dir on-click="vmA.callControllerFn();"></my-dir> 
関連する問題