score:206

Accepted answer

Angular

When angular sets up databinding two "watchers" exist (this is a simplification)

//js
$scope.name = 'test';
$timeout(function()  { $scope.name = 'another' }, 1000);
$timeout(function()  { console.log($scope.name); }, 5000);

<!-- html --->
<input ng-model="name" />

The input will start out with test, then update to another in 1000ms. Any changes to $scope.name, either from controller code or by changing the input, will be reflected in the console log 4000ms later. Changes to the <input /> are reflected in the $scope.name property automatically, since ng-model sets up watches the input and notifies $scope of the changes. Changes from the code and changes from the HTML are two-way binding. (Check out this fiddle)

React

React doesn't have a mechanism to allow the HTML to change the component. The HTML can only raise events that the component responds to. The typical example is by using onChange.

//js
render() { 
    return <input value={this.state.value} onChange={this.handleChange} />
}
handleChange(e) {
    this.setState({value: e.target.value});
}

The value of the <input /> is controlled entirely by the render function. The only way to update this value is from the component itself, which is done by attaching an onChange event to the <input /> which sets this.state.value to with the React component method setState. The <input /> does not have direct access to the components state, and so it cannot make changes. This is one-way binding. (Check out this codepen)

score:0

One way data binding is very simple, except in React we rarely use the word binding to refer how the data flows.

  const fn = (a) => { return ... }

If a value is provided as a, we'll use that value in the function scope. The above is programming 101.

  <Title name={"Hello"} />

The above line doesn't mean anything would magically happen other than the fact that "Hello" is sent to Title function and set one prop to "Hello", if you insist to use the word bind here, that's where the bind happens.

Whether you want to use this prop to display or wire with another state or whatever, you have to code yourself! There's no other magic. Btw, this is called props in React. And props is more or less a function's input argument coded in an object format. So the more accurate definition of this "bind" in React should be called assignment. In React source code, you'll see something very quickly after the element is created.

  element.props = { name: "Hello" }

And believe it or not, there's no other code in React that does anything to do with this "bind" later on.

Example

Use the input example,

<input value={value} onChange={onChange} />

If we give a value to input, the input would pick up the value to display it. If you change the value, you are intending to change the display.

Why does the value change? It can't by default. You have to change it by either listening to a system event like onChange or some business logic like setTimeout or whatever way you can ever imagine. But the change is an action, you perform the action therefore you can handle the action by changing the value. I guess this is where the "One-way" comes from. Essentially nothing is free.

Confusion

What gets us confused is that DOM element has its own state or properties. For example, we can do el.textContent="abc" without using React.

<input />

If we just code like this, we still see the value on screen changes after we type in anything. But this behavior has nothing to do with React, it's the DOM element functionalities. React refers to the first version as controlled element. Essentially React overwrites the DOM way.

NOTE

To be honest, only after I stopped using the word "binding" for these cases, I started to understand what they are. But that could be just me.

score:2

What is data binding?

data binding is a general technique that binds data sources from the provider and consumer together and synchronizes them.

Data Biding in Angular

according to AngularJs documents, Data-binding in AngularJS apps is the automatic synchronization of data between the model and view components. The view is a projection of the model at all times. When the model changes, the view reflects the change, and vice versa

enter image description here

the template (which is the uncompiled HTML along with any additional markup or directives) is compiled on the browser. The compilation step produces a live view. Any changes to the view are immediately reflected in the model, and any changes in the model are propagated to the view.

Data Binding in ReactJs

The connection between the data to be displayed in the view and the component’s logic is called data binding in ReactJS. ReactJS uses one-way data binding. In one-way data binding one of the following conditions can be followed:

  • Component to View: Any change in component data would get reflected in the view.
  • View to Component: Any change in View would get reflected in the component’s data.

enter image description here

read more here and here.

score:15

Two-way data binding provides the ability to take the value of a property and display it on the view while also having an input to automatically update the value in the model. You could, for example, show the property "task" on the view and bind the textbox value to that same property. So, if the user updates the value of the textbox the view will automatically update and the value of this parameter will also be updated in the controller. In contrast, one way binding only binds the value of the model to the view and does not have an additional watcher to determine if the value in the view has been changed by the user.

Regarding React.js, it was not really designed for two way data binding, however, you can still implement two-way binding manually by adding some additional logic, see for example this link. In Angular.JS two-way binding is a first class citizen, so there is no need to add this additional logic.

score:225

I made a little drawing. I hope it's clear enough. Let me know if it's not !

2 ways data binding VS 1 way data binding


Related Query

More Query from same tag