简体   繁体   English

如何将AngularJS变量打印为Symfony2项目中的链接

[英]How to print an AngularJS variable out as a link in a Symfony2 project

I'm trying to print out a link in symfony2 while fetching the values with AngularJS. 我试图在用AngularJS获取值的同时打印symfony2中的链接。 I am having problems with the curly brackets. 我在花括号方面遇到问题。

for example: 例如:

 <div id="columns"> <div class="block" ng-repeat = "tip in tips"> <img src="{{' {{ tip.imageDocument.webPath }}'}}" alt=""> <div class="detail"> <h2>{{ '{{ tip.title }}' }} {{ '{{ tip.id }}' }}</h2> <p>{{ '{{ tip.introduction }}'}}</p> <a href="./tips/{{'{{ tip.id }}'}}" class="button">{% trans %}Read more{% endtrans %}</a> </div> </div> </div> 

I figured that it works if I put curly brackets with around the angular variable '{{ variable '}}' 我发现如果在尖括号“ {{variable'}}”周围加上花括号,它会起作用

like this 像这样

{{ '{{ variable }}' }}

But this doesnt seem right to me. 但这对我来说似乎不对。 Also the link doesnt work. 此外,该链接不起作用。 It gets printed out as %7B%7B%20tip.id%20%7D%7D which is obvious because of the curly brackets.. 它被打印为%7B%7B%20tip.id%20%7D%7D,这显然是因为大括号。

Anyone? 任何人?

Thanks 谢谢

Did you try {{ '{{ variable }}' | raw }} 您是否尝试过{{ '{{ variable }}' | raw }} {{ '{{ variable }}' | raw }} ? {{ '{{ variable }}' | raw }} It looks like escaping is turned on in your Twig configuration. 似乎在Twig配置中启用了转义。 It is by default. 默认情况下。

EDIT : Ok, it looks like what is actually going on is that the browser is automatically "urlencoding" the curly brackets. 编辑 :好的,看来实际发生的情况是浏览器自动“大括号”大括号。 I think the issue is not with Twig, in fact it is with Angular.js. 我认为问题不在于Twig,实际上是Angular.js。 You shouldn't use href when using Angular, you should use ng-href instead. 使用Angular时不应使用ng-href而应使用ng-href

Have you tried to escape your inner curly brackets ? 您是否试图摆脱内在的花括号?

<div id="columns">

  <div class="block" ng-repeat = "tip in tips">

    <img src="{{' \{\{ tip.imageDocument.webPath \}\}'}}" alt="">

    <div class="detail">
      <h2>{{ '\{\{ tip.title \}\}' }}  {{ '\{\{ tip.id \}\}' }}</h2>

      <p>{{ '\{\{ tip.introduction \}\}'}}</p>
      <a href="./tips/{{'\{\{ tip.id \}\}'}}" class="button">{% trans %}Read more{% endtrans %}</a>
    </div>


  </div>

</div>

That's ugly but maybe this can help you. 这很丑陋,但也许可以为您提供帮助。

Use verbatim tag anyway you want to display angular's variable. 无论如何,要显示角度的变量,请使用verbatim标记。

For exemple: 举个例子:

{% verbatim %} {{ tip.title }}{% endverbatim %} 

Now {{ tip.title }} will not parsed by twig but by angularjs. 现在{{ tip.title }}不会被twig解析,而是由angularjs解析。

You should use {% verbatim %} to escape curlies from TWIG, and use ng-* attributes for src and href to allow AngularJS to parse it well, like this: 您应该使用{%verbatim%}来摆脱TWIG的curly ,并为src和href使用ng- *属性,以允许AngularJS很好地解析它,如下所示:

<div id="columns">
  <div class="block" ng-repeat="tip in tips">
    <img ng-src="{% verbatim %}{{ tip.imageDocument.webPath }}{% endverbatim %}" alt="">

    <div class="detail">
      <h2>{% verbatim %}{{ tip.title + ' ' + tip.id }}{% endverbatim %}</h2>

      <p>{% verbatim %}{{ tip.introduction }}{% endverbatim %}</p>

      <a ng-href="./tips/{% verbatim %}{{ tip.id }}{% endverbatim %}" class="button">
        {% trans %}Read more{% endtrans %}
      </a>
    </div>
  </div>
</div>

More examples about ng-* attributes 有关ng- *属性的更多示例

https://docs.angularjs.org/api/ng/directive/ngSrc https://docs.angularjs.org/api/ng/directive/ngSrc

https://docs.angularjs.org/api/ng/directive/ngHref https://docs.angularjs.org/api/ng/directive/ngHref

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

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