Lev Lev - 6 days ago 4
AngularJS Question

code in directive's controller runing although it is hidden via ng-hide

In this example:

<div ng-hide="hideMe">
<my-directive></my-directive>
</div>


I would like the code in the controller for
myDirective
, not to run when
hideMe === true
.

Is there a way to do this rather than wrapping all the code of the controller in an
if (!hideMe === true)
statement ?

Answer

ngHide simply hide the element, but if you want it not to be run you should use ngIf instead. According to the docs, ngIf removes or recreates a portion of the DOM tree (i.e., no element, no directive and no controller) but ngHide only change its visibility.

<div ng-if="hideMe">
   <my-directive></my-directive>
</div>