本文介绍了如果您在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;

所以我认为这很有效

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

08-21 16:03