0

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
  • Angular's two-way data binding happens in the client, not on the server. The advantage it gives you is that, without Angular, when trying to compile the form data, you'd need to manually go grab it out of the form element. With Angular, you have the data available to you already and only need to deal with the processing of the data. Commented Feb 27, 2016 at 23:39

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.