简体   繁体   English

如何在刀片模板的laravel函数中使用角度变量?

[英]How to use angular variables into laravel functions in a blade template?

I am using a combination of laravel and angular.js . 我正在使用laravel和angular.js的组合。 For angular I changed the interpolateProvider symbols to <% %>. 对于角度,我将interpolateProvider符号更改为<%%>。 For laravel i am using the standard {{ }} symbols. 对于laravel,我使用的是标准{{}}符号。

Now to use an angular variable in my view and pass this to a function in laravel. 现在在我的视图中使用角度变量并将其传递给laravel中的函数。

I tried this: 我尝试了这个:

{{ route('getEdit', ['id' => <% loop.id %>]) }}

Where loop.id is from an ng-repeat from angular. 其中loop.id来自角度的ng-repeat。 And route is the routing function from laravel. 而route是laravel的路由功能。

This gives an error : 这给出了一个错误:

syntax error, unexpected '<' 语法错误,意外的“ <”

You can't pass javascript variable to PHP, you will need to use Ajax. 您无法将javascript变量传递给PHP,您将需要使用Ajax。

create an action in a controller called getRoute, post [/get-route/{route}] 在名为getRoute的控制器中创建操作, post [/get-route/{route}]

PHP 的PHP

public function getRoute($request, $route) {
    return route($route, $request->input('params'));
} 

and make a request to this route with your route name and a params object : 并使用您的路线名称和params对象对此路线进行请求:

Jquery jQuery的

$.post('{{ route('getRoute') }}', {params: [{id : loop.id}]}, function(data) {
    console.log(data); //should output the route with javascript variable loop.id
});

or using angular : 或使用angular:

Angular 角度的

<script>
   var getRoute = '{{ route('getRoute') }}'; //Define your route inside a PHP file
</script>

$http.post(getRoute, {params: [{id : loop.id}]}).then(function(data) {
        console.log(data); //should output the route with javascript variable loop.id
});

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

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