Grails 和 Angular:ng-repeat 循环内的 GSP 插件标签

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

我在一个使用 Angular.

的 Grails 项目中工作

我使用一个名为 PrettyPrint 的插件来获得 "twitter like" 时间(即像 'moments ago' 这样格式化日期)。

我想在 ng-repeat 循环中使用该插件。

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

在上面的代码中,抛出的错误是Cannot get property 'date' on null object。 (它无法识别来自 angular 循环的通知项)。

如果我使用 {{notification.date}} 它会显示日期。

如何处理插件和 Angular?

简而言之,你不能。

一个是基于浏览器的,另一个是基于服务器的。您不能使用基于服务器的插件,也不能在浏览器中从 JavaScript 调用它。

考虑清楚。 ng-repeat 循环由浏览器完成。当这种情况发生时,服务器已经完成了对 GSP 的处理并将其发送到浏览器。到时候就没有机会调用插件了

从服务器获取日期并在浏览器中解析它并使用基于浏览器的插件,该插件提供与用于日期格式化的 Grails 插件相同的功能。

在您的 gsp 中尝试此代码(假设您已通过模型将通知列表传递给视图):

<%
  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>