简体   繁体   English

为什么数据绑定并不总是适用于原语?

[英]Why data binding doesn't always work with primitives?

One of the milestones in learning JS and Angular, is learning that two way data binding with primitives is tricky. 学习JS和Angular的里程碑之一是学习与原语的双向数据绑定是棘手的。 If you want to make sure that databinding works, you should always pack it into an object - that is what I was told by some seniors in my company. 如果你想确保数据绑定有效,你应该总是将它打包成一个对象 - 这就是我公司的一些老年人所说的。 My question is - why is that happening? 我的问题是 - 为什么会这样? What is exactly going on that makes databinding with primitives so compliated in JS? 究竟是什么导致数据绑定与原语在JS中如此相似?

I found out that Javascript passes objects by reverence, and primitives by value. 我发现Javascript通过崇敬传递对象,并通过值传递基元。 So what exactly happens in for example AngularJS Controller, that enables us to bind primitive in ngModel? 那么在AngularJS Controller中究竟发生了什么,它使我们能够在ngModel中绑定原语?

EDIT 编辑

Any articles, pointers are more than welcome! 任何文章,指针都非常欢迎!

Here is good post or post . 这里的帖子帖子很好。 In short: 简而言之:

"Primitive values (eg numbers, strings) are immutable in JavaScript. So whenever a change is made to them it means we are actually throwing away the previous instance and using a different one." “原始值(例如数字,字符串)在JavaScript中是不可变的。因此,每当对它们进行更改时,这意味着我们实际上丢弃了前一个实例并使用了另一个实例。”

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

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