Search code examples
angularjsangularjs-ng-include

Conditional ng-include in angularjs


How can I do the ng-include conditionally in angularJS?

For example I only want to include something if, the variable x is set to true.

<div ng-include="/partial.html"></div>

Solution

  • If you are using Angular v1.1.5 or later, you can also use ng-if:

    <div ng-if="x" ng-include="'/partial.html'"></div>
    

    If you have any older version:

    Use ng-switch:

    <div ng-switch on="x">
       <div ng-switch-when="true" ng-include="'/partial.html'"></div>
    </div>
    

    Fiddle