AngularJS:如果你没有使用(点)在你的模型,你这样做是错的? [英] AngularJS: If you are not using a .(dot) in your models you are doing it wrong?

查看:128
本文介绍了AngularJS:如果你没有使用(点)在你的模型,你这样做是错的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我记得从视频AngularJS看到这一名言说,应该使用始终。 (点)在您的模型。

I remember seeing this famous quote from a video on AngularJS saying that should be always using a . (dot) in your models.

好吧,我想按照这个说,我有

Well I am trying to follow this say I have

   var item = {}
   item.title = "Easy Access to support";
   item.available = true;
   item.price = 31.67;

因此​​,这在我看来伟大工程,我

So this works great in my view i do

  {{ item.title }}
  {{ item.available }}

我使用的是点,所以我认为这是件好事。

I am using a dot so I think this is good.

不过,我有我不考虑模型的一部分​​,但也许我错了一些属性。例如,我有我用用NG-禁用,我已经进入了这个使用点格式来启用或禁用按钮的属性。它基本上进入像这样

But I have some properties that I don't consider part of the model but maybe I am wrong. For example I have a property I use to enable or disable a button using the ng-disable, I have entered this using dot format. Its basically entered like so

 $scope.disableButton = true;

和我使用它像

 ng-disable="disableButton"......

我应该做的模型项目的一部分?或创建另一个对象的js,这样我可以用一个点持有该物业?

Should I make this part of the model "item" ? or create another js object just so i can hold this property using a dot ?

任何人都知道,如果这可以接受或者我应该做一个.DOT ??

Anybody know if this acceptable or should I be doing everything (even these simple properties) with a .dot ??

感谢

推荐答案

在应该永远是你的模型点是指 ngModel 。这个指令做双向绑定。如果您双向绑定到一个原语(如你的情况布尔),setter方法​​将设置在目前的范围,而不是定义它的范围,这可能会导致头痛,当你有一个大的用户可有很多子作用域的接口。 它不会参考其他指令,如 ngDisable 查看的关于这个具体问题的更多细节这个解释

The "there should always be a dot in your model" refers to ngModel. This directive does two-way binding. If you two-way bind to a primitive (such as a Boolean in your case), the setter will set it on the current scope rather than the scope on which it is defined, which can cause a headache when you have a large user-interface with a lot of child scopes. It does not refer to other directives such as ngDisable. See this explanation for more details on this specific issue.

示例场景:以 $ scope.foo =栏,和子作用域父母范围<输入类型=文本数据-NG-模式=foo的> 。它会显示开始,但一旦用户更改值,将在创建子作用域并结合将读取和写入该值。父母的仍将。希望总结得很好。

Sample scenario: a parent scope with $scope.foo = "bar", and a child scope with a <input type="text" data-ng-model="foo">. It will display bar initially, but once the user changes the value, a foo will be created on the child scope and the binding will read and write that value. The parent's foo will remain bar. Hope that summarises it well.

因此​​,对于 ngModel 的目的,您可能需要创建一个对象来解决这种结合的问题,但对于其他任何指令,你应该有规律,逻辑分组。

So for ngModel purposes, you might have to create an object to work around such binding issues, but for any other directive you should have the regular, logical grouping.

这篇关于AngularJS:如果你没有使用(点)在你的模型,你这样做是错的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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