Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9052959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:18:30+00:00 2026-06-16T13:18:30+00:00

I have an angular template which looks like this… <div ng-repeat=message in data.messages ng-class=message.type>

  • 0

I have an angular template which looks like this…

<div ng-repeat="message in data.messages" ng-class="message.type">

    <div class="info">
        <div class="type"></div>
        <div class="from">From Avatar</div>
        <div class="createdBy">Created By Avatar</div>
        <div class="arrowTo">
            <div class="arrow"></div>
            <div class="to">To Avatar</div>
        </div>
        <div class="date">
            <div class="day">25</div>
            <div class="month">Dec</div>
        </div>
    </div>

    <div class="main">
        <div class="content">
            <div class="heading2">{{message.title}}</div>
            <div ng-bind-html="message.content"></div>
        </div>

    </div>

    <br />
    <hr />
    <br />

</div>

I have set up a JSfiddle to show the data being bound.

What I need to do is make the “from”, “to” and “arrowTo” divs show conditionally, depending on the content of the data.

The log is is this…

  • If there is a “from” object in the data then show the “from” div and bind the data but don’t show the “createdBy” div .
  • If there is no “from” object but there is a “createdBy” object then show the “createdBy” div and bind the data.
  • If there is a “to” object in the data then show the “arrowTo” div and bind it’s data.

Or in plain English, if there is a from address, show it, otherwise show who created the record instead and if there is a to address then show that too.

I have looked into using ng-switch but I think I’d have to add extra markup which would leave an empty div if there was no data. Plus I’d need to nest switch directives and I’m not sure if that would work.

Any ideas?

UPDATE:

If I were to write my own directive (If I knew how!) then here is some pseudo code to show how I would want to use it…

<div ng-if="showFrom()">
    From Template Goes Here
</div>
<div ng-if="showCreatedBy()">
    CreatedBy Template Goes Here
</div>
<div ng-if="showTo()">
    To Template Goes Here
</div>

Each of these would disappear if the function/expression evaluated to false.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T13:18:31+00:00Added an answer on June 16, 2026 at 1:18 pm

    Angular 1.1.5 introduced the ng-if directive. That’s the best solution for this particular problem. If you are using an older version of Angular, consider using angular-ui’s ui-if directive.

    If you arrived here looking for answers to the general question of “conditional logic in templates” also consider:

    • 1.1.5 also introduced a ternary operator
    • ng-switch can be used to conditionally add/remove elements from the DOM
    • see also How do I conditionally apply CSS styles in AngularJS?

    Original answer:

    Here is a not-so-great “ng-if” directive:

    myApp.directive('ngIf', function() {
        return {
            link: function(scope, element, attrs) {
                if(scope.$eval(attrs.ngIf)) {
                    // remove '<div ng-if...></div>'
                    element.replaceWith(element.children())
                } else {
                    element.replaceWith(' ')
                }
            }
        }
    });
    

    that allows for this HTML syntax:

    <div ng-repeat="message in data.messages" ng-class="message.type">
       <hr>
       <div ng-if="showFrom(message)">
           <div>From: {{message.from.name}}</div>
       </div>    
       <div ng-if="showCreatedBy(message)">
          <div>Created by: {{message.createdBy.name}}</div>
       </div>    
       <div ng-if="showTo(message)">
          <div>To: {{message.to.name}}</div>
       </div>    
    </div>
    

    Fiddle.

    replaceWith() is used to remove unneeded content from the DOM.

    Also, as I mentioned on Google+, ng-style can probably be used to conditionally load background images, should you want to use ng-show instead of a custom directive. (For the benefit of other readers, Jon stated on Google+: “both methods use ng-show which I’m trying to avoid because it uses display:none and leaves extra markup in the DOM. This is a particular problem in this scenario because the hidden element will have a background image which will still be loaded in most browsers.”).
    See also How do I conditionally apply CSS styles in AngularJS?

    The angular-ui ui-if directive watches for changes to the if condition/expression. Mine doesn’t. So, while my simple implementation will update the view correctly if the model changes such that it only affects the template output, it won’t update the view correctly if the condition/expression answer changes.

    E.g., if the value of a from.name changes in the model, the view will update. But if you delete $scope.data.messages[0].from, the from name will be removed from the view, but the template will not be removed from the view because the if-condition/expression is not being watched.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Warning: Angular newbie ahead. I have this code in an angular.js page <div class=clearfix
have written this little class, which generates a UUID every time an object of
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
have a problem. At first look at this HTML <div id=map style=background-image: url(map.png); width:
Hi there I have this confirmable button directive which I am working on, The
I have this code class DownloadView(TemplateView): template_name = 'pdfform/create_form2.html' def serve_pdf(self, request): #pdf_data =
I have various angular services which call a rest api on the server. If
I'm confused, I have this module which routes to different controllers: var mainModule =
I have this module routes: var mainModule = angular.module('lpConnect', []). config(['$routeProvider', function ($routeProvider) {
I'm trying to dynamically assign a controller for included template like so: <section ng-repeat=panel

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.