My problem is similar to the one in this question, This works when we know the scope variable to remove. However I am looking for something generic, like remove the variable for all the elements this directive is attached to, whenever they become hidden. I tried injecting the ngModel and tried setting to null or delete, doesn't seem to work.
This is what I am trying to to: plunk
myModule.directive('destroyY', function(){
return{
restrict:'A',
require: '?ngModel',
link: function(scope, elem, attrs, ngModel) {
scope.$on('$destroy', function(){
console.log(ngModel);
ngModel=null; // doesn't work
delete ngModel; // doesn't work
})
}
}
});
Any help is greatly appreciated. Thanks!