单击时如何从控制器获取数据到指令

Mid*_*sai 5 javascript angularjs

我试图在我使用时将数据发送到控制器的指令链接功能ng-clicked,但是我没有将数据发送到指令,该指令在第一次加载页面时调用

这是我的HTML代码

 <h2 ng-click="clickMe(somedata)"> click </h2>

    <my-directive my-key="myVal"> </my-directive>
Run Code Online (Sandbox Code Playgroud)

这是我的控制器

.controller('myController',function($scope){

$scope.clickMe=function(somedata){
$scope.myVal=somedata;
};

});
Run Code Online (Sandbox Code Playgroud)

我的指示

.directive('myDirective',function(){
return{
  restrict:'E',
  scope:{
    myKey:'='
        },
  templateUrl:'exampleTempl.html',
  controller:'myController',
  link:function(scope,elem,attr){
   console.log(scope.myKey); // getting undefined
   console.log(scope.myVal); //here i want the data from controller when i clicked on ng-click
  }
}
});
Run Code Online (Sandbox Code Playgroud)

kem*_*ğlu 2

我希望这个对你有用;

.directive('myDirective',function(){
return{
  restrict:'E',
  scope:{
    myKey:'=bindingmyVal'
        },
  templateUrl:'exampleTempl.html',
  controller:'myController',
  link:function(scope,elem,attr){
   console.log(scope.myKey); //here i want the data from controller when i clicked on ng-click
  }
}
});
Run Code Online (Sandbox Code Playgroud)