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 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签)
  • ¥50 sft下载大文阻塞卡死
  • ¥15 机器人轨迹规划相关问题