I have a simple JSFiddle:
<div ng-controller="MainCtrl as mainCtrl">
<div ng-show="mainCtrl.visible" ng-click="mainCtrl.swap()">Can you see me?</div>
<div see-me></div>
</div>
angular.module('AngularTestApp', [])
.controller('MainCtrl', [function () {
var self = this;
self.visible = true;
self.swap = function() {
self.visible = ! self.visible;
};
}])
.directive('seeMe', [function () {
return {
template: 'or me?',
link: function (scope, element, attrs) {
attrs.$set('ng-show', 'mainCtrl.visible');
attrs.$set('ng-click', 'mainCtrl.swap()');
}
};
}]);
Since the default value for a scope
on a directive's definition object is false
I would expect the parent's scope to be available and thus for attrs.$set('ng-click', 'mainCtrl.swap()');
to work, but it does not fire when I click the div
. Why?
(N.B. I tried adding $compile
as per ppa's answer to 'AngularJS - ng-click in directive's link function' but that had no effect.)