隐藏表单字段在 Angular Js 中不起作用

Hidden form field not working in Angular Js

这是我在 laravel 应用程序中的 html 表单,它有隐藏字段,这些隐藏值需要发送到 angular js 控制器。

<form accept-charset="UTF-8" enctype="multipart/form-data">
    <input name="_token" type="hidden"  value="{{ csrf_token() }}">
    <input name="user_id" type="hidden"  value="{{ Auth::user()->id }}">
    <input name="post_id"  type="hidden" value="<% post.id %>" >
    <input name="published_at" type="hidden" value="{{ Carbon\Carbon::today()->format('Y-m-d') }}">
    <input class="form-control" placeholder="comment"  ng-model="contentField" type="text" >
    <button type="submit" ng-click="addComment()">comment</button>
</form>

我的Angular控制器如下

 $scope.addComment = function(){
     var comment = {
       user_id: $scope.user_id,
       content: $scope.contentField,
       post_id: $scope.post_id,
       published_at: $scope.published_at
 };

我只能在控制器中获取 contentField 的值,请帮我解决这个问题!

您似乎缺少隐藏导入的 ng-model 标签。因此 angular 不知道如何或在何处将隐藏的输入值绑定到 $scope。将这些标签添加到所有隐藏的输入应该可以解决您在 $scope.

上找不到它们的问题

我们可以这样简单地尝试:

<input type="hidden" ng-model="post_id" ng-init="post_id=<% post.id %>"/>

<input type="hidden" ng-model="post_id" value="{{post_id}}" ng-init="post_id=<% post.id %>"/>

<input type="hidden" ng-model="post_id" value="<% post.id %>" ng-init="post_id=<% post.id %>"/>

注意:这里 ng-init 正在执行绑定来自另一个模型或对象或直接值的任何技巧:)