duannian3494 2017-09-08 18:52
浏览 56

如何将Symfony变量传递给AngularJS

I'm trying to obtain from the symfony 3 variable to AngularJS to modify and show it, but how I can "share" from the Symfony to AngularJS?.

Let me explain this way When I pass from the Symfony $this->render to the twig and using {{ name.firstName }} it show/works even if I'm using the dump or only the var, but I don't have idea to how pass to AngularJS the data of the variable "name.firstName" to modify in AngularJS, can anybody help me ???

(to clarify I'm not using JQuery or another thing, only JS, AngularJS, and Symfony3)

Here my code:

CheckController.php

/**
     * @Route("/payment", name="PaymentCheck.payment")
     * @return Response
     */
    public function indexAction(Request $request): Response
    {
        $customer = $this->getCustomer();

        /.../ more code /.../

        return $this->render(':PaymentCheck:index.html.twig', [
            'customerProfile' => $customer,  //<---- variable
            'order' => $recalculatedOrder->getOrder(),
            'form' => $clickToCallForm->createView(),
        ]);
    }

index.twig.html

    <div ng-if="shippingOptions.currentTab == 'homeDelivery'">
        <div class="address-book-section">
          <div class="choose-address">{{ 'PaymentCheck.select_shipping_address'|trans }}</div>
          {# it show, but how can I pass the variable to AngularJS? the customerProfile one #}
          {{ dump(customerProfile) }} 
          <address-book type="shipping" creatable="true" selectable="true"></address-book>
          </div>

          <div ng-repeat="package in PaymentCheck.order.packages">
                {% include ':PaymentCheck:itemList.html.twig' with {onlyUndeliverables: true} %}
        </div>
    </div>
  • 写回答

1条回答 默认 最新

  • dpepbjp126917 2017-09-08 20:51
    关注

    The good, the bad, and the Ugly

    The good

    In app and your Controllers, set a variable to populate Angular Variables in your main layout template. Keep in mind that you should be able to drastically improve on this to make it much cleaner, and out of most of your controllers. This example is a bit ugly, for the sake of simplicity.

    Controller

    return $this->render(':Checkout:index.html.twig', [
        'angular' => [
             'customerProfile' => $customer,
        ],
        'order' => $recalculatedOrder->getOrder(),
        'form' => $clickToCallForm->createView(),
    ]);
    

    Layout

    <script type="text/javascript">
        SymfonyDataForAngular = {
            {% for key, value in angular %}
                {{ key }}: '{{ value }}',
            {% endfor %}
        };
    </script>
    

    .. then you can call SymfonyDataForAngular.customerProfile from javascript anywhere.

    The bad

    Create an ajax request in Angular to return the data you need in json. This might actually wind up being the best approach, as long as you structure it so you'll only make one AJAX call for each user action.

    Controller

    public function getCustomerAjax()
    {
        return new JsonResponse([
            'customerProfile' => $this->getCustomer()
        ]);
    }
    

    The ugly

    Create the view variables in the controller, dump those into javascript variables for Angular to access.

    I'm not showing an example for this, because if people start doing it, everyone will hate me. Besides, it's close to the first example I already gave.

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器