0

I'm trying to build a monthly calendar using Angular.js, and I can't seem to find a way of outputting expressions when conditions are satisfied.

  <tr>
    <td ng-repeat="date in dates">
      {{date}}
    </td>
    {{'</tr><tr>' | if date.getDay() === 0}}
  </tr>

Or

  <tr>
    <td ng-repeat="date in dates">
      {{date}}
      {{ if date.getDay() === 0 expr = '</tr><tr>' else expr = ''}}
    </td>
    {{expr}}
  </tr>

How could I achieve this?

Thanks.

5
  • Your code implies your date to be an object. If so, {{date}} would render the string representation of that object. Commented Dec 6, 2013 at 17:19
  • I just copied some example code from my project, it doesn't matter if it's a date object. Commented Dec 6, 2013 at 17:22
  • @fdomig answer is a good choice. Use ng-repeat. If your collection needs filtering, add a filter in the ng-repeat. Commented Dec 6, 2013 at 17:26
  • That's what I did, but it doesn't answer my question. Commented Dec 6, 2013 at 17:32
  • I think you are set on one specific direction. You can solve your issue by taking an alternative that might be cleaner and easier to read. You should let the data drive the DOM without trying to manipulate the DOM markup directly. Commented Dec 6, 2013 at 18:50

1 Answer 1

2

You could use the built in ng-if directive.

<tr ng-if="date.getDay() === 0">

The better solution could be a nested ng-repeat so you don't even have to do these checks. For instance:

<tr ng-repeat="week in weeks">
    <td ng-repeat="day in week.days">...</td>
</td>
Sign up to request clarification or add additional context in comments.

3 Comments

I can't use ng-if, because adding a tr tag wouldn't help me, I need to close the tag and open it, so I can get to the next row.
You can use the ng-if in both, the <tr> and </tr> tag.
To add to @fdomig, add a filter to filter days that getDay() return zero.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.