繁体   English   中英

Laravel 5.6-POST和AngularJS的混合

[英]Laravel 5.6 - Mixture of POST and AngularJS

old()返回angularjs数据时,如何在Laravel刀片中正确输出“ number:4”到“ 4”?

情境

我正在使用Laravel路由发布对象

 <form method="POST" action="/pricestructures/create"> <input name="_method" type="hidden" value="POST"> {{ csrf_field() }} <div class="form-group"> <label for="">Name</label> <input type="text" name="name" class="form-control" placeholder="Name" required value="{{old('name')}}" required> </div> <div class="form-group"> <label for="">Client</label> <select name="client_id" ng-model="data.client_id" ng-options="o.id as o.name for o in Clients" class="form-control" required ng-change="GetAssets()"> <option value="">Please select</option> </select> </div> <div class="form-group"> <label for="">Asset</label> <select name="asset_id" ng-model="data.asset_id" ng-options="o.id as o.name for o in Assets" class="form-control" required ng-disabled="!data.client_id"> <option value="">Please select</option> </select> </div> <div class="form-group"> <label for="">Quantity</label> <input type="number" name="quantity" class="form-control" placeholder="Quantity" required min="0" step="1" value="{{old('quantity')}}" required> </div> <div class="form-group"> <label for="">Price</label> <input type="number" name="price" class="form-control" placeholder="Price" required min="0" step="0.01" value="{{old('price')}}" required> </div> <div class="form-group"> <button type="submit" class="btn btn-primary">Save Changes</button> </div> </form> 

该表单位于AngularJS 1.6控制器中,当它使用GetAssets()更改时,我监视client_id select并下载相关资产。 因为我为此使用AngularJS,所以我将client_id和asset_id字段的值都存储在$ scope中。

同样,当返回任何错误时,我想使用old()重新填充$ scope变量。

$scope.data = {
    client_id: {{ old('client_id',0) }},
    asset_id: {{ old('asset_id',0)}}
};

提交表单时,我可以看到正在发送的数据正在“键入”,好吧,无论如何,与选择下拉列表相关的值都是

表格数据

如前所述,如果存在提交问题,我使用old()重新用提交的数据填充表单,但是对于client_idasset_id变量(即Angular JS变量),我将返回“ number:1”,但我没有确定如何将其转换为“ 1”。

如果我碰巧将angularjs变量存储为字符串,它们将被发布为“ string:1”

在您的控制器中尝试

$scope.data = {
    client_id: '{{ old('client_id',0) }}',
    asset_id: '{{ old('asset_id',0)}}'
};

只需设置双引号或单引号,就会出现实际值。

暂无
暂无

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

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