dqalnwuci494308 2016-02-27 23:35
浏览 42
已采纳

双向绑定PHP和AngularJS

I am building an AngularJS App, with a PHP RESTful API for the Backend. I am wondering what is the best way to take advantage of the 2-Way Data Binding of AngularJS with my example.

For example, I have a checkbox called: "Activate Newsletter", which is linked to a column in my MySQL database: Here is my HTML Code:

<input type="checkbox" ng-model="newsletter"> Activate NewsLetter

(newsletter is a variable from the scope of my Controller)

What is the best way to send a request to my PHP Server, using 2 Way Data Binding, to make it change instantly when the user checks/unchecks this box.

Thank you!

  • 写回答

1条回答 默认 最新

  • dongxiaoyan4388 2016-02-27 23:38
    关注

    Make an AJAX request do the server with the checkbox state (your newsletter scope variable). However, I'd suggest having a small delay before making the actual in order to avoid making tons of requests if users start clicking the checkbox rapidly. Of course, during the delay, you'd have to check if there's a previous delay active and kill it so that you only keep the latest checkbox value.

    So, the easiest way is to put the AJAX request code as an ng-click callback. Nothing special Angular can do here for you because the two-way binding happens inside the client app (your browser) while you still need server requests to communicate with the server.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程