Angular
When angular establishes data binding, there are two "observers" (this is a simplification)
//js $scope.name = 'test'; $timeout(function() { $scope.name = 'another' }, 1000); $timeout(function() { console.log($scope.name); }, 5000); <input ng-model="name" />
Login will start with test , and then upgrade to another in 1000 ms. Any changes to $scope.name , either from the controller code or by changing the input, will be displayed in the console log 4000 ms later. Changes to <input /> are automatically reflected in the $scope.name , since ng-model sets the hours of input and notifies $scope of the changes. Changes to code and changes from HTML are two-way binding . (Check out this script )
To react
React does not have a mechanism that allows HTML to modify a component. HTML can only raise events that the component responds to. A typical example is the use of onChange .
//js render() { return <input value={this.state.value} onChange={this.handleChange} /> } handleChange(e) { this.setState({value: e.target.value}); }
The value of <input /> completely controlled by the render function. The only way to update this value is with the component itself, which is done by attaching the onChange event to <input /> , which sets this.state.value using the React setState component method. <input /> does not have direct access to the state of components and therefore cannot make changes. This is a one way binding . (See codepen )
Tyrsius Dec 29 '15 at 22:37 2015-12-29 22:37
source share