2014-10-08 51 views
0

我有一個directive與代碼如何從指令觸發事件的角度

seriesClick: function (e) { 
       //console.log(e.category); 
       // use scope.$emit to pass it to controller 
       scope.$emit('feederValue', e.category); 
      }, 

在我controller我使用

 $scope.$on('feederValue', function (evt, value) { 
      console.log(value); 
      $scope.feederFitBoundValue = value; 
      $scope.getFitBoundForFeederID($scope.feederFitBoundValue); 
     }); 

我想在不同的指令 我來定義這個功能想用它

我的代碼

function getFitBoundForFeederID(feederFitBoundValue){ 
    alert(feederFitBoundValue); 
} 

我收到錯誤TypeError: undefined is not a function

我的項目是在Angular框架我想使用的$emit$on

邏輯請建議,如果任何的Optimus解決方案。我只是想將One指令的值傳遞給不同的指令。同時我想從一個指令傳遞一些值到另一個指令。我想要做事件觸發。

回答

0

一個簡單的解決方法是發出$rootScope上的事件並在$rootScope上觀看此事件,但我不確定這是否是最好的方式。

0

要將值從一個指令傳遞到另一個指令,可以使用factory來創建保存共享數據的服務。

app.factory("sharedDataService", function() { 
      return { 
       yourData: "anything" 
      }; 
     });