The answers are correct, and the sample code you provided works for small problems, but when the solution to this problem is executed in large applications, you can use this approach:
Updated pen
<div ng-app="myApp" ng-controller="MainController" class="container-fluid"> <h2 ng-bind="currentVersion"></h2> <hr> <div dirr value="{{hidden}}">Default div</div> </div> .directive('dirr', function($log){ var directiveInstance = { restrict:'AE', scope:{ value:'@' }, link: function (scope, element, attrs, ctrl) { if(attrs.value === 'true'){ console.log('directive on !!'); $log.debug('element',element); element[0].innerHTML = '<div>hello ' + attrs.value + '</div>'; } else { console.log('directive off !!'); } } } return directiveInstance; })
This is tidier, and you cannot duplicate your code using the ngIf or ngSwitch directives in separate divs when you have something like:
<table> <thead dirr value="{{statement}}"> <tr> <th> CrazyStuffHere... </th> <th> CrazyStuffHere... </th> .... </tr> </thead> </table>
source share