简体   繁体   English

当与angularJS中的ng-repeat一起使用时,javascript [],[[]]和[{}]之间的工作差异

[英]Working difference between javascript [],[[]] and [{}] when used with ng-repeat in angularJS

I have created a form to add multiple users information dynamically. 我创建了一个表单来动态添加多个用户信息。 It working fine. 它工作正常。 But there is a question in my mind about working of array and array of object inside ng-repeat. 但是在我的脑海中存在一个问题,即在ng-repeat.中使用arrayarray of object ng-repeat. If I use $scope.users = []; 如果我使用$scope.users = []; and $scope.users=[{}] , ng-repeat works differently. $scope.users=[{}]ng-repeat工作方式不同。 With $scope.users = [], view appears blank, and 使用$scope.users = [], view appears blank, $scope.users = [{}], view appears correctly. $ scope.users = [{}],视图正确显示。 I have created fiddle link for both. 我为两者创建了小提琴链接。 Can anyone explain me what exactly happening here. 任何人都可以解释我到底发生了什么。

Link with $scope.users = [ ], is: http://jsfiddle.net/9Ymvt/2799/ 与$ scope.users = []的链接是: http//jsfiddle.net/9Ymvt/2799/
Link with $cope.users = [{ }] , is : http://jsfiddle.net/9Ymvt/2800/ 链接$cope.users = [{ }] ,是: http//jsfiddle.net/9Ymvt/2800/

Update: If I use $scope.users = [[]] , It works fine, but the data is not binding first time, as it is binding in $scope.users = [{}] . 更新:如果我使用$scope.users = [[]] ,它工作正常,但数据第一次没有绑定,因为它绑定在$scope.users = [{}] Data only binds when I click ob addMore users button 当我单击ob addMore users button时,数据才会绑定
Here is the Fiddle link: http://jsfiddle.net/9Ymvt/2801/ 这是小提琴链接: http//jsfiddle.net/9Ymvt/2801/

What I am thinking , If the data is binding with [{}] , why not with [[]] ; 我在想,如果数据与[{}]绑定,为什么不与[[]]绑定;

$cope.users = [{ }] have one object inside its blank {} , so ng-repeat will iterate through the objects inside the array. $cope.users = [{ }]在其空白{}内有一个对象,因此ng-repeat将遍历数组内的对象。 doesn't matter the objects are empty or not. 对象是空的无关紧要。 here your ng-repeat runs for 1 time. 这里你的ng-repeat运行了一次。

but in 但在

$scope.users = [ ] , users array don't have anything inside, there is nothing to iterate , so ng-repeat is not going to iterate. $scope.users = [ ]users array里面没有任何东西,没有什么要迭代,所以ng-repeat不会迭代。

[{}] is an array with an empty object so it's length is 1 and looping over it will happen while [] is an empty array with length 0 so a loop will not run over it. [{}]是一个带有空对象的数组,所以它的长度为1,并且会在它上面循环,而[]是一个长度为0的空数组,因此循环不会在它上面运行。 Hence, the form is populated when you run with [{}] since angular can loop over the array. 因此,当您使用[{}]运行时,将填充表单,因为angular可以循环遍历数组。

Update: It is not a matter of [] Vs{}. 更新:这不是[] Vs {}的问题。 {} is equal to new Object() and [] is equal to new Array(). {}等于new Object(),[]等于new Array()。 The outer array, user that you are looping on, attempts to bind the current element of the loop which is person in your case (from below snippet) 外部数组,您正在循环的用户,尝试绑定循环的当前元素,这是您案例中的人(从下面的代码段)

ng-repeat="person in users"

to person.name. 到person.name。 However, the .name can only be attached to an object and not an array so the binding fails when you use [[]] since angular tries to attach the .name to the array inside. 但是,.name只能附加到一个对象而不是一个数组,所以当你使用[[]]时绑定失败,因为angular尝试将.name附加到里面的数组。

First one $scope.users = [] is empty array, and the second $scope.users = [{ }] is just an array with one object. 第一个$scope.users = []是空数组,第二个$scope.users = [{ }]只是一个包含一个对象的数组。 When you do ng-repeat it iterates through all objects inside array, and in first case array is empty, that's why view is empty. 当你执行ng-repeat时,它会遍历数组中的所有对象,并且在第一种情况下,数组是空的,这就是为什么视图为空的原因。

this: 这个:

$scope.users =  [{ }]

equals to this: 等于这个:

$scope.users =  [];
$scope.users.push({ });

it's just declaration of array with item inside. 它只是声明带有项目的数组。

Its pretty simple :), 它非常简单:),

$scope.users = [] is an empty array While $scope.users = [{}] is an array with empty object. $ scope.users = []是一个空数组而$ scope.users = [{}]是一个空对象的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM