简体   繁体   English

如何将Symfony变量传递给AngularJS

[英]How to pass Symfony variable to 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?. 我正在尝试从symfony 3变量获取AngularJS进行修改并显示它,但是如何从Symfony“共享”到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 ??? 让我这样解释一下,当我从Symfony $ this-> render传递到树枝并使用{{name.firstName}}时,即使我使用转储或仅使用var,它也可以显示/运行,但是我不这样做有一个想法如何在AngularJS中将变量“ name.firstName”的数据传递给AngularJS进行修改,有人可以帮我吗?

(to clarify I'm not using JQuery or another thing, only JS, AngularJS, and Symfony3) (以说明我没有使用JQuery或其他东西,只有JS,AngularJS和Symfony3)

Here my code: 这是我的代码:

CheckController.php 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 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>

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. ..然后您可以从任何地方的javascript调用SymfonyDataForAngular.customerProfile

The bad 不好

Create an ajax request in Angular to return the data you need in json. 在Angular中创建一个ajax请求,以返回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. 实际上,这可能是最好的方法,只要您对它进行结构化,以便对每个用户操作仅进行一次AJAX调用即可。

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. 在控制器中创建视图变量,将其转储到javascript变量中以供Angular访问。

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. 此外,它已经接近我已经给出的第一个示例。

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

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