简体   繁体   English

Angular4真的是2向数据绑定吗?

[英]Is Angular4 really 2-Way data-Binding?

What I have learned of Angular2/4/5/6 is that due to [(ngModel)] the 2-way data binding is possible in it -- Just like it happend in Angular1 via $scope. 我对Angular2 / 4/5/6的了解是,由于[[ngModel)] ,可以在其中进行2向数据绑定-就像它通过$ scope在Angular1中发生一样。

Now, I came across this website which clearly states that 2-Way data binding is not possible in Angular4. 现在,我碰到了这个网站,该网站明确指出Angular4中无法进行2向数据绑定。 I am highly confused. 我很困惑。 Can someone really clarify that -- whether 2-Way data binding is possible or Not in Angular 2/4/5/6? 有人真的可以澄清一下吗?在Angular 2/4/5/6中,是否可能进行2-Way数据绑定?

在此处输入图片说明

Link: https://dzone.com/articles/understanding-output-and-eventemitter-in-angular 链接: https//dzone.com/articles/understanding-output-and-eventemitter-in-angular

在此处输入图片说明

whether 2-Way data binding is possible or Not in Angular 2/4/5/6? 在Angular 2/4/5/6中是否可以进行2向数据绑定?

Nope, it is not [(ngModel)] is not two-way data binding. 不,不是[(ngModel)]不是双向数据绑定。 It's just a syntactic sugar for a simultaneous event binding and property binding. 它只是用于同时进行事件绑定和属性绑定的语法糖。

To Explore more on this 进一步探索

1. Do you really know what unidirectional data flow means in Angular 1. Do you really know what unidirectional data flow means in Angular

2. Change Detection Reinvented Victor Savkin 2. Change Detection Reinvented Victor Savkin
3. TWO-WAY DATA BINDING IN ANGULAR 3. TWO-WAY DATA BINDING IN ANGULAR

Angular is a javascript framework and javascript has limitations that does not allow real two-way databinding. Angular是一个JavaScript框架,并且javascript有一些限制,不允许进行真正的双向数据绑定。

Indeed, the first way (template to variable) is made possible thanks to native javascript API that allows to listen to events on input fields, but there is not such thing as events emitted when a variable changes, so the other way is not feasible with the javascript API. 事实上,第一种方式(模板变量)是可能由于本地JavaScript API,允许收听输入字段事件,但没有这样的东西发出的事件,当一个变量发生变化,因此,另一种方法是不是可行javascript API。

Here comes the detection change , made possible by Zone.js, Angular "listens" to your code and triggers a change detection on these three types of events: 这是Zone.js实现的检测更改 ,Angular“侦听”您的代码,并针对这三种类型的事件触发更改检测:

  • User event on page (click, submit, blur, etc...) 页面上的用户活动(点击,提交,模糊,等...)
  • XMLHttpRequest has ended XMLHttpRequest已经结束
  • A timer ( setTimeout or setInterval ) triggered some function 计时器( setTimeoutsetInterval )触发了某些功能

The change detection updates the template with every bound variable. 更改检测会使用每个绑定变量来更新模板。

That's how the two-way databinding virtually exists. 那怎么绑定双向实际上存在。

And by the way [(ngModel)] is syntaxic sugar for [ngModel]="variable" (ngModelChange)="variable = $event" : these are the two ways 顺便说一句[(ngModel)]是syntaxic糖[ngModel]="variable" (ngModelChange)="variable = $event" :这是两个方面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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