【问题标题】:How to call a PHP variable from JavaScript in laravel?如何在 laravel 中从 JavaScript 调用 PHP 变量?
【发布时间】:2014-06-30 10:54:39
【问题描述】:

我正在尝试将服务的经度和纬度传递给生成谷歌地图的脚本。 这是我的代码:

刀片

<script
src="http://maps.googleapis.com/maps/api/js?key=AIzaSyDY0kkJiTPVd2U7aTOAwhc9ySH6oHxOIYM&sensor=false">
</script>
<script>
function initialize() {
var mapOptions = {
scaleControl: true,
center: new google.maps.LatLng({{$servicio->Longitud}}}, {{$servicio->Latitud}}},
zoom:18
};

var map = new google.maps.Map(document.getElementById('map-canvas'),
  mapOptions);

var marker = new google.maps.Marker({
map: map,
position: map.getCenter()
});
var infowindow = new google.maps.InfoWindow();
google.maps.event.addListener(marker, 'click', function() {
  infowindow.open(map, marker);
});
}

google.maps.event.addDomListener(window, 'load', initialize);
</script>

<div class="perfiles">
<legend>Detalle de servicio</legend>
<table class="table">
    <tr><td>Numero de Orden</td><td>{{$servicio->idServicio}}</td></tr>
    <tr><td>Cliente</td><td>{{$servicio->Cliente}}</td></tr>
    <tr><td>Fecha Inicio</td><td>{{$servicio->Hora_Inicio}}</td></tr>
</table>
@if($servicio->Completado)
    <div id="map-canvas" style="width:650px;height:300px;"></div>
    <table>
        <tr><td>{{HTML::image($servicio->RutaFoto1, '', array('width' => '300', 'height' => '300'))}}</td><td>{{HTML::image($servicio->RutaFoto2, '', array('width' => '300', 'height' => '300'))}}</td></tr>
        <tr><td>{{HTML::image($servicio->RutaFoto3, '', array('width' => '300', 'height' => '300'))}}</td><td>{{HTML::image($servicio->RutaFoto4, '', array('width' => '300', 'height' => '300'))}}</td></tr>
    </table>
@endif

控制器

public function doDetail($id){
    $servicio = Servicio::find($id);
    return View::make('detalleservicio', array('servicio' => $servicio));

}

问题是脚本无法识别代码 laravel 或 php。我该如何解决?

【问题讨论】:

    标签: javascript php laravel-4


    【解决方案1】:

    在经度和纬度之后有三个 }}}。我相信经度应该是}},纬度应该是}})

    center: new google.maps.LatLng({{$servicio->Longitud}}}, {{$servicio->Latitud}}},
    

    应该是:

    center: new google.maps.LatLng({{$servicio->Longitud}}, {{$servicio->Latitud}}),
    

    【讨论】:

      【解决方案2】:

      您应该使用双大括号{{}} 或三重大括号{{{}}}。您不应该将一种与另一种混用。

      代替

      center: new google.maps.LatLng({{$servicio->Longitud}}}, {{$servicio->Latitud}}},
      

      你应该这样做

      center: new google.maps.LatLng({{$servicio->Longitud}}, {{$servicio->Latitud}},
      

      如果您要使用 {{{ 三个大括号 }}} 而不是 {{ 两个 }},那么您的输出将被转义,尖括号将被转换为 HTML 实体。

      您可以在此处阅读有关使用 Blade 模板引擎回显数据的更多信息:http://laravel.com/docs/templates#other-blade-control-structures

      【讨论】:

        猜你喜欢
        • 2020-02-01
        • 2017-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-16
        • 2018-01-30
        相关资源
        最近更新 更多