【问题标题】:Laravel 5.6 - Mixture of POST and AngularJSLaravel 5.6 - POST 和 AngularJS 的混合
【发布时间】:2018-08-28 21:21:46
【问题描述】:

问题

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 选择并下载相关资产。因为我为此使用 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 变量),我将返回“数字: 1”,我不确定如何将其变成“1”。

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

【问题讨论】:

  • 你有没有考虑过使用php中的explode函数来分割字符串,使用“:”作为分隔符。然后将正确的东西返回给刀片。这应该在 php 控制器上完成
  • 嗨托比,是的,我已经考虑过了,但我认为这可能不是处理这个问题的“正确”方式。 “打字”显然是故意的,所以我认为有一种解码方式(类似于 json_decode)但我不确定这是什么类型的编码。

标签: php angularjs laravel-5


【解决方案1】:

在你的控制器中尝试

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

只需设置双引号或单引号,就会出现真正的值。

【讨论】:

    猜你喜欢
    • 2017-07-17
    • 2014-07-30
    • 2015-05-29
    • 2018-11-13
    • 1970-01-01
    • 2012-02-10
    • 1970-01-01
    • 1970-01-01
    • 2015-07-08
    相关资源
    最近更新 更多