简体   繁体   English

在laravel刀片模板中写一个js表达式

[英]Write a js expression inside laravel blade template

I am using tagsinput for some tagging operation. 我正在使用tagsinput进行一些标记操作。 I want to add some default value to the tag field that is sent from controller. 我想为从控制器发送的标记字段添加一些默认值。 But it seems that the way i am trying to use the js expression is not working. 但似乎我试图使用js表达式的方式不起作用。 Can anyone help me. 谁能帮我。 Here is code. 这是代码。

<script>
    var cities = new Bloodhound({
                    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('text'),
                    queryTokenizer: Bloodhound.tokenizers.whitespace,
                    local: members
                });
                cities.initialize();
    var elt = $('#researcher');
                elt.tagsinput({
                    itemValue: 'value',
                    itemText: 'text',
                    allowDuplicates: false,
                    typeaheadjs: {
                        name: 'cities',
                        displayKey: 'text',
                        source: cities.ttAdapter()
                    }
                });
    @foreach ($memberResearch->member as $member)
         {!! $text = $member->firstName." ".$member->lastName !!}
         {!! $id = $member->member_id !!}
         @if($member->pivot->role == "Researcher")
             elt.tagsinput('add' , {"value" : {{ $id }} , "text" :{{ $text }} });
         @endif
    @endforeach 
</script>

PHP and Javascript are two very seperate entities. PHP和Javascript是两个非常独立的实体。

For this kind of stuff, the best approach is for laravel to dump the contents (the data) into the page somewhere where the frontend (javascript) can then pick up. 对于这种东西,最好的方法是将laravel将内容(数据)转储到页面某处,然后前端(javascript)可以拾取。

For example: 例如:

    // controller
    $appConfig = [ 'foo' => 'bar' ];

    // blade
    <script>
        var AppConfig = {!! ! empty($appConfig) ? json_encode($appConfig) : "{}" !!};
    </script>

    // js
    var foo = JSON.parse(AppConfig).foo;
    console.log(foo); // outputs bar

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

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