duanliang9288 2014-04-23 08:05 采纳率: 100%
浏览 76

使用ng-click发布表单(旧方法)是不是这种做法?

I have been given a task by my boss who still likes things the old way... have a HTML page/view and submitting a form to a PHP page server side. We have introduced AngularJS to his app and he has the following code in his page:

<form name="rForm" id="rForm"  action="demo_form.php" method="post">
    <input type="hidden" name="quantity" id="reservationQuantity" ng-value="selectedQuantity"/>
    <input type="hidden" name="date" id="reservationDate" ng-value="selectedDate"/>
    <input type="hidden" name="time" id="reservationTime" ng-value="selectedTime"/>
    <input type="hidden" name="mId" id="mID" value="{{ $merchant->id }}" ng-value="mId"/>
</form>

<button>Go to next page...</button>

Now he wishes for us to have a link or button to submit this form which is outside the form, I believe that we should control all of this is a controller with all post functionality being handled by an injected / separate $http service. He doesn't believe me and says he knows best and I should just handle submitting the form like so

<button onclick="$('#reservationForm').submit();">Go to next page...</button>

I don't like this... however he pays the wages. Is there some way I can submit the form/page from my Angular controller and remove all that horrible jQuery code? Something like this (this won't work, I am just using this for visual purposes)

<button ng-click="ng-submit()">Go to next page...</button>

... and am I right saying that his method is bad practice and if we are going to submit to the server by pushing the page there's no point in us using an MVC framework.

  • 写回答

1条回答 默认 最新

  • doufenyu7610 2014-04-23 08:19
    关注

    Yes you can submit form through your Angular Controller. For this all you have to do is to call controller function in your ng-click like this:

    <button ng-click="submit()">Go to next page...</button>
    

    where submit() is your controller function. One more thing you need to assign ng-model to all form fields to access them in controller.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大