Search code examples
angularjsif-statementternary-operator

inline conditionals in angular.js


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?


Solution

  • 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"}}