AngularJS:在NG-点模型 [英] AngularJS: dot in ng-model

查看:140
本文介绍了AngularJS:在NG-点模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在视频 AngularJS MTV Meetup网站:最佳实践(2012年12月11日),MISKO解释说,..如果你用NG-模式必须有一个点的地方,如果你没有一个点,你就错了。

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网站第一个示例(基础知识)似乎违背它。是什么赋予了?已Angular.JS因为MTV聚会,它现在更多的宽容与NG-模式改变?

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视频点有一个很好的概述,做到这一点非常流行的堆栈溢出问题:<一href=\"http://stackoverflow.com/questions/14049480/what-are-the-nuances-of-scope-prototypal-prototypical-inheritance-in-angularjs/14049482#14049482\">What范围是原型/原型继承的细微差别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?

我将在这里总结一下吧:

I'll try to summarize it here:

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-模式=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.

发生这种情况的原因是因为孩子Scope使用原型继承来查找值,所以只要它永远不会对孩子设置,那么它会推迟到父范围。但是,一旦它的设置,它不再查找父。

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.

当你使用一个对象(巴兹),而不是,从来都没有被设置在子范围,产业依然存在。

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

有关更深入的细节,请查看<一个href=\"http://stackoverflow.com/questions/14049480/what-are-the-nuances-of-scope-prototypal-prototypical-inheritance-in-angularjs/14049482#14049482\">StackOverflow回答

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

这篇关于AngularJS:在NG-点模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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