【问题标题】:Trying to concatenate an AngularJS var with a Laravel Blade var尝试将 AngularJS var 与 Laravel Blade var 连接起来
【发布时间】:2015-06-25 16:38:36
【问题描述】:

我正在开发一个 Laravel 应用程序。首先,我使用 FormBuilder 制作了一个表单,所以我有了这个:

{!! Form::open(['method' => 'delete', 'route' => ['clientes.destroy', $cliente->id]]) !!}

当我决定开始使用 AngularJS 时,我意识到我必须删除 FormBuilder,因为现在我必须使用 Angular 打印“客户 ID”(使用 ng-repeat)。所以我把我的代码改成了这个(如果你不知道 Laravel 忽略隐藏的输入):

<form method="POST" action="{{ route('clientes.destroy') }}/@{{ cliente.id }}">
    <input name="_method" type="hidden" value="DELETE">
    <input name="_token" type="hidden" value="{{ csrf_token() }}">

问题是 @{{ cliente.id }} 正在破坏我的应用程序,因此它不显示我的客户。

更多上下文:此表单位于客户表中,并显示一个用于删除客户的按钮。

我应该如何打印变量?

编辑:如果我从表单中删除 action 属性,则会显示所有客户端,因此我 100% 确定问题出在此连接上。

EDIT2:这行得通

<form method="POST" action="@{{ cliente.id }}">

这也有效

<form method="POST" action="{{ route('clientes.destroy') }}">

EDIT3:整个中继器。

<tr ng-repeat="cliente in buscador.clientes">
    <td>@{{ cliente.tipo.nombre }}</td>
    <td>@{{ cliente.nombre }}</td>
    <td>@{{ cliente.direccion  }}</td>
    <td>@{{ cliente.telefono  }}</td>
    <td>@{{ cliente.email  }}</td>
    <td>@{{ cliente.rubro  }}</td>
    <td>
    <form method="POST" action="@{{ '{{ route('clientes.destroy') }}/' + cliente.id }}">
        <input name="_method" type="hidden" value="DELETE">
        <input name="_token" type="hidden" value="{{ csrf_token() }}">
        <button class="btn btn-danger"><i class="fa fa-trash-o"></i></button>
    </form>
</td>

【问题讨论】:

  • 看起来{{ route('clientes.destroy') }} 没有被 PHP 解析并替换为字符串。
  • 你确定你有这个表单在刀片模板中,而不是在角度模板中?
  • 所有这些代码都在 index.blade.php 中,正如您在 EDIT2 中看到的那样,它们都单独工作,当我尝试连接它们时出现问题
  • 所以请点击Ctrl+U查看源码,应该与PHP生成的&lt;form&gt;标签一致。在此处粘贴该行。
  • 用你给定的代码输出这个:
    (没有解析)

标签: angularjs templates laravel laravel-5 blade


【解决方案1】:

1.首先你需要更改AngularJS花括号不与Blade模板引擎冲突:

var app = angular.module('app', []) 

  .config(function($interpolateProvider) {
    // To prevent the conflict of `{{` and `}}` symbols
    // between Blade template engine and AngularJS templating we need
    // to use different symbols for AngularJS.

    $interpolateProvider.startSymbol('<%=');
    $interpolateProvider.endSymbol('%>');
  });

您当前正在使用@{{ }},这是冲突的根源,这就是 PHP 无法正确解析您的代码的原因。我建议使用&lt;%= %&gt;,因为它是常用的结构,你可以在Underscore templates找到它。

2.那么,从Angular 1.2.x开始,你可以bind only one expression as action.

所以,这段代码应该可以工作:

<form 
  method="POST" 
  action="<%= '{{ route('clientes.destroy') }}/' + cliente.id %>">

【讨论】:

【解决方案2】:

无需修改任何内容的更好解决方案是:

action="{{url('urlportal')}}/@{{cliente.id}}"

【讨论】:

    猜你喜欢
    • 2019-08-24
    • 1970-01-01
    • 2017-09-24
    • 2023-04-09
    • 2016-07-20
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 1970-01-01
    相关资源
    最近更新 更多