【发布时间】:2026-02-18 17:20:04
【问题描述】:
这是我在 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 的值,请帮我解决这个问题!
【问题讨论】:
-
我会放弃使用 type="hidden" 和 value=".." 来支持角度指令 ng-hide="true" 和 ng-model="..."。将值设置为等于某物不会将其绑定到角度模型。此外,将您的评论变量作为 $scope.comment 放在 $scope 上,并直接绑定到它,如 ng-model="comment.user_id"
-
还有其他方法可以在不使用 ng-model 的情况下发送隐藏的表单值吗?
标签: angularjs laravel angularjs-scope hidden-field