为什么AngularJS文档在model指令中不使用点? [英] Why don't the AngularJS docs use a dot in the model directive?

查看:54
本文介绍了为什么AngularJS文档在model指令中不使用点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在视频中 AngularJS MTV聚会:最佳做法(2012/12/11),Miško解释说:"..如果您使用ng-model,则必须在某处有一个点.如果没有点,那么您就做错了."

In the video AngularJS MTV Meetup: Best Practices (2012/12/11), Miško explains "..if you use ng-model there has to be a dot somewhere. If you don't have a dot, you're doing it wrong.."

但是, Angular.JS网站中的第一个示例(基础知识)似乎与此矛盾.是什么赋予了?自从MTV聚会以来,Angular.JS是否已更改,因为它现在对ng-model更加宽容了?

However, the very first example (The Basics) in the Angular.JS website seems to contradict it. What gives? Has Angular.JS changed since the MTV meetup that it's now more forgiving with ng-model?

推荐答案

在处理范围继承的复杂性时,这个小点非常重要.

That little dot is very important when dealing with the complexities of scope inheritance.

egghead.io视频"The Dot" 具有很好的概述,非常流行的堆栈溢出问题: AngularJS中范围原型/原型继承的细微差别是什么?

The egghead.io video "The Dot" has a really good overview, as does this very popular stack overflow question: What are the nuances of scope prototypal / prototypical inheritance in AngularJS?

我将在这里总结一下:

Angular.js使用范围继承来允许子范围(例如子控制器)查看父范围的属性.因此,假设您有一个类似这样的设置:

Angular.js uses scope inheriting to allow a child scope (such as a child controller) to see the properties of the parent scope. So, let's say you had a setup like:

<div ng-controller="ParentCtrl">
    <input type="text" ng-model="foo"/>
    <div ng-controller="ChildCtrl">
        <input type="text" ng-model="foo"/>
    </div>
</div>

(在JSFiddle上玩)

首先,如果您启动了该应用程序,并键入了父输入项,则子项将进行更新以反映它.

At first, if you started the app, and typed into the parent input, the child would update to reflect it.

但是,如果您编辑子作用域,则与父作用域的连接现在断开,并且两者不再同步.另一方面,如果使用ng-model="baz.bar",则链接将保留.

However, if you edit the child scope, the connection to the parent is now broken, and the two no longer sync up. On the other hand, if you use ng-model="baz.bar", then the link will remain.

发生这种情况的原因是,子作用域使用原型继承来查找值,因此,只要从未在子作用域上设置该值,它就将顺从父作用域.但是,一旦设置,它将不再查找父级.

The reason this happens is because the child scope uses prototypical inheritance to look up the value, so as long as it never gets set on the child, then it will defer to the parent scope. But, once it's set, it no longer looks up the parent.

当您使用对象(baz)时,子作用域上一无所有,继承仍然存在.

When you use an object (baz) instead, nothing ever gets set on the child scope, and the inheritance remains.

有关更多详细信息,请查看

For more in-depth details, check out the StackOverflow answer

这篇关于为什么AngularJS文档在model指令中不使用点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆