简体   繁体   中英

Two Way Binding PHP and 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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM