inline conditionals in angular.js
Solution 1
EDIT: 2Toad's answer below is what you're looking for! Upvote that thing
If you're using Angular <= 1.1.4 then this answer will do:
One more answer for this. I'm posting a separate answer, because it's more of an "exact" attempt at a solution than a list of possible solutions:
Here's a filter that will do an "immediate if" (aka iif):
app.filter('iif', function () {
return function(input, trueValue, falseValue) {
return input ? trueValue : falseValue;
};
});
and can be used like this:
{{foo == "bar" | iif : "it's true" : "no, it's not"}}
Solution 2
Angular 1.1.5 added support for ternary operators:
{{myVar === "two" ? "it's true" : "it's false"}}
Solution 3
Thousands of ways to skin this cat. I realize you're asking about between {{}} speifically, but for others that come here, I think it's worth showing some of the other options.
function on your $scope (IMO, this is your best bet in most scenarios):
app.controller('MyCtrl', function($scope) {
$scope.foo = 1;
$scope.showSomething = function(input) {
return input == 1 ? 'Foo' : 'Bar';
};
});
<span>{{showSomething(foo)}}</span>
ng-show and ng-hide of course:
<span ng-show="foo == 1">Foo</span><span ng-hide="foo == 1">Bar</span>
<div ng-switch on="foo">
<span ng-switch-when="1">Foo</span>
<span ng-switch-when="2">Bar</span>
<span ng-switch-default>What?</span>
</div>
A custom filter as Bertrand suggested. (this is your best choice if you have to do the same thing over and over)
app.filter('myFilter', function() {
return function(input) {
return input == 1 ? 'Foo' : 'Bar';
}
}
{{foo | myFilter}}
Or A custom directive:
app.directive('myDirective', function() {
return {
restrict: 'E',
replace: true,
link: function(scope, elem, attrs) {
scope.$watch(attrs.value, function(v) {
elem.text(v == 1 ? 'Foo': 'Bar');
});
}
};
});
<my-directive value="foo"></my-directive>
Personally, in most cases I'd go with a function on my scope, it keeps the markup pretty clean, and it's quick and easy to implement. Unless, that is, you're going to be doing the same exact thing over and over again, in which case I'd go with Bertrand's suggestion and create a filter or possibly a directive, depending on the circumstances.
As always, the most important thing is that your solution is easy to maintain, and is hopefully testable. And that is going to depend completely on your specific situation.
Solution 4
I am using the following to conditionally set the class attr when ng-class can't be used (for example when styling SVG):
ng-attr-class="{{someBoolean && 'class-when-true' || 'class-when-false' }}"
The same approach should work for other attribute types.
(I think you need to be on latest unstable Angular to use ng-attr-, I'm currently on 1.1.4)
I have published an article on working with AngularJS+SVG that talks about this and related issues. http://www.codeproject.com/Articles/709340/Implementing-a-Flowchart-with-SVG-and-AngularJS
Solution 5
For checking a variable content and have a default text, you can use:
<span>{{myVar || 'Text'}}</span>
Related videos on Youtube
user1469779
Updated on July 08, 2022Comments
-
user1469779 almost 2 years
I was wondering if there is a way in angular to conditionally display content other than using ng-show etc. For example in backbone.js I could do something with inline content in a template like:
<% if (myVar === "two") { %> show this<% } %>
but in angular, I seem to be limited to showing and hiding things wrapped in html tags
<p ng-hide="true">I'm hidden</p> <p ng-show="true">I'm shown</p>
What is the recommended way in angular to conditionally show and hide inline content in angular just using {{}} rather than wrapping the content in html tags?
-
Ben Lesh over 9 yearsplease unaccept my answer an accept 2Toad's when you get a chance.
-
-
user1469779 over 11 yearsThank you blesh, this is what I was going for. However I notice that if I try to put an html tag in one of the values it breaks: {{thing.second == "two" | iif : "<ul class='two-class'>" : "<ul>"}} I realize that there may be better ways to do this in angular, but is there a way to escape "<" and ">" so that tags can be output as part of the string?
-
Ben Lesh over 11 yearsngBind doesn't allow HTML output, you'd want to use ng-bind-html-unsafe
-
user1469779 over 11 yearsThe example of ng-binf-html-unsafe in the angular documentation uses it within a tag, for example <ANY ng-bind-html-unsafe="{expression}">. it may not be possible to do what I am trying to do inline.
-
Ben Lesh over 11 yearsI've honestly not tried that. To me something feels wrong about writing out HTML in that manner. But I don't know your specific use case.
-
iConnor over 10 yearsWhat's the reason for
iif
and notif
? -
Ben Lesh over 10 yearsIIF is the abbreviation for "Inline If". It's common in different programming languages... just not as common as ?: inline ifs. ;)
-
Dave Everitt over 10 years
ui-if
was removed at least from latest version angular-ui but since angular 1.1.5 you haveng-if
(from this comment) -
Code Whisperer over 9 yearsThis answer with the most upvotes should appear at the top of the answers... it's by far the most correct
-
Filip Kis over 9 yearsuser1469779 consider accepting this answer as this is the recommended way of achieving what you want for quite some time
-
Ben Lesh over 9 years@2Toad, my answer was old. this is the correct answer now, I don't know if the user will be back to "accept" it now, but I've annotated my answer as such. Such is the changing face of software development.
-
Josh over 9 yearsThanks. How can I have it display the value of
myVar
only if it is false? (i.e. how can I nest variables in the expression?) I tried with{{myVar === "two" ? "it's true" : {{myVar}}}}
but it doesn't work. -
2Toad over 9 years@Josh the
myVar
property doesn't need to be wrapped in additional curly braces, it's already inside an expression. Try{{myVar === "two" ? "it's true" : myVar}}
-
Nick Coad over 9 years@BenLesh major props for editing your answer now that there's other options, good work.
-
Simona Adriani about 9 yearsthank you! I was trying this but I misses the quotes around the string :-)
-
asherrard over 8 yearsjust used this in the wild in a directive to show or hide a show image link with the ternary with Angular 1.4.3 thank you much good sirs and ladies!
<span ng-if="product.image.split('/').pop() !== 'product' ? true : false" class="img-thumb wh-logo-thumb">
-
aoakeson almost 8 yearsFor whatever reason, this syntax does not work using one time bindings.. {{::myVar || 'Text'}} will not work. Must be without the ::
-
eran otzap about 7 yearscan i use it with an evaluation of a function call ? {{foo == myObj.IsSomething | iif : "::GetTrueText()" : "::GetFalseText()"}} ?
-
Tom Stickel almost 7 yearsWorked for me , i did
{{vm.StateName === "AA" ? "ALL" : vm.StateName}}
-
zokaee hamid about 5 yearsmy example : <h4 class="modal-title" > {{ Item.Title == Null ? "bela"+ CurrentCat.Title : Item.Title}}</h4>
-
zokaee hamid about 5 yearsand : <button type="submit" class="btn btn-default" ng-click="Ok(Item)" ng-hide="Item.Title == Null ? true : false" data-dismiss="modal">bela</button>