简体   繁体   中英

Concatenate Javascript variable inside laravel blade curly braces

I want to concatenate javascript variable within blade {{ curly braces }} as following:

Currently:

ajax: "{{ route(api.colors) }},"

What I want:

var page='colors';
...
ajax: "{{ route(api."+page+") }},"

Is it possible?

You cannot do this directly because the curly brackets are rendered on the server and javascript runs on the client side. You could put a placeholder in you route and then replace this part in your javascript code. Like so:

// Imagine the `api.page` route value is `/controller/{page}`:
ajax: "{{ route(api.page) }}".replace("{page}", page);

You can try this code, it worked for me.

 var id = $('#user_id').val();
 console.log(id);   
 ajax: "{{ URL::to('/meme') }}/poster/"+id+"",

You can use the ziggy library .

It has a helper function route(route_name) that you can easily use for this purpose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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