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.

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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器