Search code examples
angularjsgrailsangularjs-ng-repeatgspprettytime

Grails and Angular: GSP plugin tag inside ng-repeat loop


I'm working in a Grails project that uses Angular.

I use a plugin called PrettyPrint in order to have "twitter like" time (i.e. formatting dates like 'moments ago').

I want to use the plugin inside a ng-repeat loop.

<tr ng-repeat="notification in notifications">
...
    <td><prettytime:display date="${notification.date}" /></td>
...
</tr>

In the above code, the error thrown is Cannot get property 'date' on null object. (it doesn't recognize notification item from angular loop).

If I use {{notification.date}} it shows the date.

How can I deal with both, the plugin and Angular?


Solution

  • Try this code in your gsp (assume you have passed notifications list to the view via the model):

    <%
      def notifications = notificationsFromModel.collect {
        [date: prettyTime.display(it.date), message: it.message]
      }
    %>
    
    <span ng-init="notifications = <%= notifications as JSON %>"></span>
    
    <tr ng-repeat="notification in notifications">
      <td>{{ notification.date }}</td>
      <td>{{ notification.message }}</td>
    </tr>