如果你没有在你的 AngularJS 模型中使用 .(dot) 你做错了吗? [英] If you are not using a .(dot) in your AngularJS models you are doing it wrong?

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

问题描述

我记得在一个关于 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;

所以我认为这很好用

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

我使用了一个点,所以我认为这很好.

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

但是我有一些属性我不认为是模型的一部分,但也许我错了.例如,我有一个使用 ng-disable 启用或禁用按钮的属性,我使用点格式输入了它.基本上是这样输入的

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 = "bar" 的父作用域,以及具有 <input type="text" data-ng-model=" 的子作用域foo">.它最初会显示 bar,但是一旦用户更改了值,将在 子作用域 上创建一个 foo 并且绑定将读取和写那个值.父对象的 foo 将保持为 bar.希望总结得很好.

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 模型中使用 .(dot) 你做错了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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