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>
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>
I encountered a similar issue and may be this finding can help someone. I have to display different partials on click of link but ng-if and ng-switch both were not working in this scenario(Below code was not working).
So what I did is, instead of using ng-if, On click of link just update the value of partialArticleUrl (which is a model in controller) and declare below code on html.
You could also do
In your controller
A little bit more readable version of one of the answers. Plus setting
ng-include
tonull
removes the element - just likeng-if
.If you are using Angular v1.1.5 or later, you can also use ng-if:
If you have any older version:
Use ng-switch:
Fiddle
If the condition is simple enough, you could also put the conditional logic directly in the ng-include expression:
Note the expression
x
is not in single quotes and therefore evaluated. See http://plnkr.co/edit/4fvNDa6Gm3dbVLgsAZrA for more details.