假设您的示例是您的问题的简化,以下是您如何在这两个指令之间进行通信:
app.directive('barchart', function($log) {
return {
restrict: 'EA',
controller: function($scope) {
this.drawBar = function(height, date) {
$log.log('[barchart] Handling charting of bar with label ' + date + ' and height ' + height + '.');
};
},
link: function(scope, element, attrs) {
$log.log('[barchart] Doing DOM transformations...')
}
};
});
app.directive('bar', function($log) {
return {
restrict: 'EA',
require: '^barchart', // Require an immediate parent element of barchart
// Requiring the barchart directive gives us access to the bar chart controller instance.
link: function(scope, element, attrs, barchartCntrl) {
barchartCntrl.drawBar(scope.row.height, scope.row.date);
}
};
});
工作的笨蛋