Reputation: 4993
What should be the right behaviuor when you have a ng-model
declaration like this one?
data-ng-model="elements[0]"
The way it works, if elements
is already defined in the scope as an array, it works as I'd expected assigning the first element of the array.
But if elements
is not declared it assigns this value :
elements = {0:'anyvalue'}
(which makes sense if I'd had written data-ng-model="elements['0']"
)
In this case :
elements[0]='anyvalue';
elements['0']='anyvalue';
and I cannot read the value of the propery using "dot" notation (elements.0
or elements.'0'
).
So it looks correct, but a bit weird.
Is this behaviour correct, or it should instantiate an array when the scope variable is not defined?
Upvotes: 0
Views: 39
Reputation: 2800
An array is just a special type of object. If you look at an array in a debugger, all of the values are listed as properties with numeric keys, like the one you show. If you don't initialize the object as an array, it would still accesses the object in the same way, which just means you now have an object with numeric keys and none of the helpful functions from the Array prototype.
Upvotes: 1