如何在Laravel-5中将变量传递给JavaScript?

有没有什么方法可以让JavaScript从小型控制器Laravel-5中获得变量?

代码如下:

   $langs = Language::all();
   return View::make('NAATIMockTest.Admin.Language.index',compact('langs'));
public function create()
       {
          $names = $this->initLang();
          Javascript::put([
            'langs' => $names            ]);

            return View::make('NAATIMockTest.Admin.Language.create',compact('names'));
    }
     public function edit($id)
     {
        //
          $lang = Language::findOrFail($id);
          $names = $this->initLang();
          Javascript::put([
            'langs' => $names            ]);

           return View::make('NAATIMockTest.Admin.Language.edit', compact('lang'));
           // return View::make('NAATIMockTest.Admin.Language.edit');

    }

视图创建:

 @extends('AdLayout')
     @section('content')
   <  script type=text/javascript>
      var app = angular.module('myApp', []);
      app.controller('langCtrl', function($scope) {
          $scope.languages = langs;
      });
   </script>

   <div class=container-fluid ng-app=myApp ng-controller=langCtrl>
      <div class=row>
        <div class=col-md-8 col-md-offset-2>
            <div class=panel panel-default>
                <div class=panel-heading>
                    <h2>Create language                    </h2>
                </div>

                <div class=panel-body>
                    {!! Form::open() !!}
                        <p class=text-center>
                            {!! Form::label('Name','Language: ') !!}
                            <input type=text name=searchLanguage ng-model=searchLanguage>
                        </p>

                        <select name=Name[] multiple size=10 ng-model=lang ng-click=show()>
                            <option value=@{{v}} ng-repeat=(k,v) in languages | filter:searchLanguage>
                                @{{v}}
                            </option>
                        </select><br>

                        <div class=text-center>
                            {!! Form::submit('Create',['class'=>'btn btn-primary']) !!}&nbsp;
                              {!!   Html::linkAction('NAATIMockTest\LanguageController@index', 'Back', null, array('class' => 'btn btn-primary')) !!}
                        </div>
                    {!! Form::close() !!}
                </div>
            </div>
        </div>
    </div>
    </div>
   @endsection

视图编辑:

 @extends('AdLayout')
    @section('content')
    <script type=text/javascript>

    var app = angular.module('myApp', []);
    app.controller('langCtrl', function($scope) {
        $scope.languages = langs;
    });
   </script>

   <div class=container-fluid ng-app=myApp ng-controller=langCtrl>
       <div class=row>
          <div class=col-md-8 col-md-offset-2>
              <div class=panel panel-default>
                <div class=panel-heading><h2>Edit #{{ $lang->id}}</h2></div>
                <div class=panel-body>
                    {!! Form::model($lang) !!}
                        {!! Form::label('Name','Language: ') !!}&nbsp;{{ $lang->Name }}
                        <!-- {!! Form::text('Name',null,['disabled']) !!}<br> -->
                        {!! Form::hidden('id') !!}<input type=text name=searchLanguage ng-model=searchLanguage>
                        </p>

                        <select name=Name[] size=10>
                            <option value=@{{v}} ng-repeat=(k,v) in languages | filter:searchLanguage>
                                @{{v}}
                            </option>
                        </select><br>
                        <div class=text-center>
                            {!! Form::submit('Update',['class'=>'btn btn-info']) !!}&nbsp;
                            {!! Html::linkAction('NAATIMockTest\LanguageController@index', 'Back', null, array('class' => 'btn btn-primary')) !!}
                        </div>
                    {!! Form::close() !!}
                </div>
            </div>
        </div>
    </div>
   </div>
   @endsection

配置文件夹中的javascript.php:

<?php    return [

    /*
    |--------------------------------------------------------------------------
    | View to Bind JavaScript Vars To
    |--------------------------------------------------------------------------
    |
    | Set this value to the name of the view (or partial) that
    | you want to prepend all JavaScript variables to.
    |
    */
    'bind_js_vars_to_this_view' => 'footer',
    'bind_js_vars_to_this_view' => 'NAATIMockTest.Admin.Language.create',
    'bind_js_vars_to_this_view' => 'NAATIMockTest.Admin.Language.edit',

    /*
    |--------------------------------------------------------------------------
    | JavaScript Namespace
    |--------------------------------------------------------------------------
    |
    | By default, we'll add variables to the global window object. However,
    | it's recommended that you change this to some namespace - anything.
    | That way, you can access vars, like SomeNamespace.someVariable.
    |
    */
    'js_namespace' => 'window',];

解决方法

试试以下方法:

<script>
    var sampleTags = [];
    @foreach ($services as $service)
        sampleTags.push('{{ $service->name }}');
    @endforeach</script>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


laravel的dd函数不生效怎么办
看不懂laravel文档咋办
安装laravel框架出现command怎么办
Laravel开发API怎么使用事务
laravel怎么构建复杂查询条件
laravel如何实现防止被下载
为什么laravel比yii火
一些常见的Laravel定时任务不运行的问题
laravel用路由有什么好处
composer无法安装laravel怎么办
laravel现在还用吗
laravel怎么替换主键id
laravel的appurl有什么用
如何修改Laravel的报错输出形式
laravel怎么避免foreach查表
laravel怎样操作数据库
laravel怎么截取字符串
laravel 是国内的吗
laravel怎么设置请求头
浅析Laravel社区Redis组件报错的问题和解决方法