Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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> 
like image 760
JustGoscha Avatar asked Apr 12 '13 14:04

JustGoscha


People also ask

What is Ng include in AngularJS?

AngularJS ng-include Directive The ng-include directive includes HTML from an external file. The included content will be included as childnodes of the specified element. The value of the ng-include attribute can also be an expression, returning a filename.

What is templateUrl in AngularJS?

templateUrl can also be a function which returns the URL of an HTML template to be loaded and used for the directive. AngularJS will call the templateUrl function with two parameters: the element that the directive was called on, and an attr object associated with that element.

What is ngInit?

The ngInit directive allows you to evaluate an expression in the current scope. This directive can be abused to add unnecessary amounts of logic into your templates. There are only a few appropriate uses of ngInit : aliasing special properties of ngRepeat , as seen in the demo below.


1 Answers

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

like image 159
Mark Rajcok Avatar answered Oct 22 '22 22:10

Mark Rajcok