When we assign a value to the scope and trigger the digest, the UI will be updated simultaneously
scope.text ='zzz';
scope.$digest();
Directly assigning values to the scope will not update the UI. Angular will update the UI only when the digest is triggered. In most cases, Angular actively triggers the digest for us, giving the impression that we can just give the scope casually. The result A situation arises that you cannot understand. For example
First of all, let me make a statement. I am not very familiar with the angularjs mentioned in the question, which is a bit wrong. Knockout also has the same two-way data binding feature. It is implemented in the observer mode. After creating the observed object, when assigning value using the set method, it will also notify observers who are interested in the object. To achieve the effect of two-way data binding.
Implement a simple example similar to two-way binding
HTML
SCRIPT
Update data when view changes
This is very simple, just bind the event when compiling the instruction myModel
Data changes update UI
There are watchers and digest inside scope
When we assign a value to the scope and trigger the digest, the UI will be updated simultaneously
Using setTimeout directly, the result is that the UI is not updated, and we need to do the following processing
Or use angular’s $timeout, which helps us trigger the digest
Let’s search. There are many articles that can help you answer your questions. Search first and then ask questions
First of all, let me make a statement. I am not very familiar with the angularjs mentioned in the question, which is a bit wrong.
Knockout also has the same two-way data binding feature. It is implemented in the observer mode. After creating the observed object, when assigning value using the set method, it will also notify observers who are interested in the object. To achieve the effect of two-way data binding.
angular1 is a dirty check. . This also brings about performance problems
How does AngularJS implement its two-way data binding mechanism?
可以参考:
http://teropa.info/build-your-own-angular/build_your_own_angularjs_sample.pdf