简体   繁体   English

如何在Angular JS中使用Grails消息代码标签

[英]How to use Grails Message code tag with Angular JS

I have a custom directive that loads .GSP template ie 我有一个加载.GSP模板的自定义指令,即

In my directive I have 在我的指令中

template: '/view/pages/dummy.gsp'

In dummy.gsp, 在dummy.gsp中,

I have a checkbox like below: 我有一个如下复选框:

<input type="checkbox" name="orangeFruit"> Orange </input>

Now here instead of using the hardcoded Orange I want to use something like this : 现在在这里,而不是使用硬编码的Orange,我想使用这样的东西:

<div ng-repeat="thisfruit in fruits">
<input type="checkbox" name="{{thisfruit}}chkbox">
    ${message(code:'label.{{thisfruit}}')}
</input>
</div>

Above snippet is of my angular template where I am iterating through list of fruits and putting checkboxes for each one of them. 上面的代码片段是我的角度模板,在其中我要遍历水果列表并为每个水果放置复选框。

fruits:["orange","apple","banana"] is the angular JSON object.

where {{thisfruit}} is Javascript object and has the value orange. 其中{{thisfruit}}是Javascript对象,值为橙色。

Below is how my messages.properties file looks like: 以下是我的messages.properties文件的外观:

label.orange=Orange
label.apple=Apple
label.banana=Banana

When I run the above message code it always gives me "label.orange" instead of "orange". 当我运行上面的消息代码时,它总是给我“ label.orange”而不是“ orange”。 I have this key in my messages.properties file so it should find it. 我在messages.properties文件中有此密钥,因此应该可以找到它。 when I replace label.{{thisfruit}} with the label.orange it gives the correct value. 当我将label.{{thisfruit}}替换为label.orange它会给出正确的值。

Any help is appreciated! 任何帮助表示赞赏!

This is not possible at all and is a most common problem people used to get confused on. 根本不可能做到这一点,这是人们经常困惑的最常见问题。 You are basically trying to fix a server side GSP or Groovy code with client side javascript or angular code. 您基本上是在尝试使用客户端JavaScript或角度代码来修复服务器端GSP或Groovy代码。

GSP file gets compiled at server side not at browsers so it does no about any angular code like {{thisfruit}} which will never be compiled at server side. GSP文件是在服务器端而不是浏览器上编译的,因此它不会处理任何像{{thisfruit}}这样的棱角代码,而这些代码永远不会在服务器端编译。

Similarly when GSP's are compiled into HTML code it will get converted to HTML code which a browser understands how to render it and there your angular code gets executed. 类似地,将GSP编译为HTML代码后,它将转换为HTML代码,浏览器会理解该HTML代码如何呈现它,然后执行角度代码。

So when it renders at client side there will be no code available like ${message(code:'label.{{thisfruit}}')} you are trying to execute since it has already got compiled at server side before rendering. 因此,当它在客户端渲染时,将没有可用的代码,例如${message(code:'label.{{thisfruit}}')} ,因为它已经在渲染之前在服务器端进行了编译。

So you can not use server side (Grails) message code at client side (angular) code. 因此,您不能在客户端(角度)代码中使用服务器端(Grails)消息代码。

Well for that you can use a pretty awesome client side library for angular. 为此,您可以使用一个很棒的客户端库来实现角度。 http://angular-translate.github.io/ http://angular-translate.github.io/

Check it out. 看看这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM