DEMO// Code goes here
angular.module('app', [])
.controller('FooCtrl', FooController)
.controller('BarCtrl', BarController)
.directive('foo', fooDirective)
.directive('bar', barDirective);
FooController.$inject = ['$window'];
function FooController($window) {
this.notify = function() {
$window.alert('foo ctrl');
};
}
FooController.$inject = ['$window'];
function BarController($window) {
this.notify = function() {
$window.alert('bar ctrl');
};
}
function fooDirective() {
return {
controller: 'FooCtrl'
};
}
function barDirective() {
return {
controller: 'BarCtrl',
controllerAs: 'ctrl',
require: 'foo',
link: function(scope, elem, attrs, ctrl) {
if (elem[0].tagName === 'A') {
elem.on('click', function() {
ctrl.notify();
});
} else {
elem.on('click', function() {
scope.ctrl.notify();
});
}
}
};
}