Hey there! I've got this cool directive that lets you conveniently hide or show elements:
<div cdt-visible-to="Admin,Moderator">...</div>
By using this directive, our HTML code becomes more declarative. Here's a snippet of what the directive implementation looks like:
eDiscovery.directive('cdtVisibleTo', ['AuthService', function (AuthService) {
return {
restrict: 'AE',
link: function ($scope, elem, attrs) {
let cdtArray = String(attrs['cdtVisibleTo'] || '')
.split(/[ ,]+/).map(i => String(i).trim()).filter(i => i);
let ln = cdtArray.length;
for (let i = 0; i < ln; i++) {
let r = cdtArray[i];
if(AuthService.hasPersona(r)){
elem.removeAttr('hidden');
return;
}
}
elem.attr('hidden', 'hidden');
}
}
}]);
This directive acts similar to ng-show
, but I'm curious about creating a replacement for ng-if
.
What would be the most efficient way to completely remove elements from the DOM using an Angular directive?