var app = angular.module('stackOverflow', []);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.commonControl = {};
});
app.directive('commondir', [
'$timeout',
function($timeout){
return {
restrict: 'EA',
scope: {
commonControl: '='
},
link: function(scope, element, attributes) {
scope.recordControl = {
start: function(testVar) {
console.log('start');
scope.recordControl.onStart(testVar);
$timeout(function(){
scope.recordControl.onStop(testVar);
}, 2000);
},
stop: function(testVar) {
console.log('stop');
scope.recordControl.onStop(testVar);
}
};
scope.commonControl.getRecordControl = function() {
return scope.recordControl;
}
}
};
}
]);
app.directive('directiveA', [
function(){
return {
restrict: 'EA',
scope: {
recordControl: '=',
testVar: '='
},
link: function(scope, element, attributes) {
scope.recordControl.onStart = function(testVar) {
console.log('onStart', testVar);
};
scope.recordControl.onStop = function(testVar) {
console.log('onStop', testVar);
};
element.bind('click', function(){
console.log(scope.recordControl);
scope.recordControl.start(scope.testVar);
});
}
};
}
]);
app.directive('directiveB', [
function(){
return {
restrict: 'EA',
scope: {
recordControl: '=',
testVar: '='
},
link: function(scope, element, attributes) {
scope.recordControl.onStart = function(testVar) {
console.log('onStart', testVar);
};
scope.recordControl.onStop = function(testVar) {
console.log('onStop', testVar);
};
element.bind('click', function(){
console.log(scope.recordControl);
scope.recordControl.start(scope.testVar);
});
}
};
}
]);
<!DOCTYPE html>
<html ng-app="stackOverflow">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js" ></script>
</head>
<body ng-controller="MainCtrl">
<p>Hello {{name}}!</p>
<div commondir common-control="commonControl">Common</div>
<div directive-a record-control="commonControl.getRecordControl()" test-var="'a'">Directive A</div>
<div directive-a record-control="commonControl.getRecordControl()" test-var="'a1'">Directive A1</div>
<div directive-b record-control="commonControl.getRecordControl()" test-var="'b'">Directive B</div>
<div directive-b record-control="commonControl.getRecordControl()" test-var="'b1'">Directive B1</div>
</body>
</html>
У меня тоже есть один и тот же вопрос? Есть ли решение для этого ?? –