0

Sometimes you need to ng-if or ng-show an item in html based on some choices made earlier. One of these for me is "Additional Item". You can enter one set of information, and also if you want, an additional set. This creates an array of 2 similar objects. With this setup, you can only have 1 or 2 objects in this array. (important, since the scope of this question needs to be limited this way)

I want to ng-show an html directive based on "myItemsArray.length > 1". Since the array can (read should) only be 1 or 2 in length (not 0), this should work. However, it does not, because AngularJS seems to be adding an item "proto" to the array which adds to the count. See the image.

enter image description here

The problem is, proto makes the array length equal 2. I am not going to just look for length > 2 because i really don't know if i can count on proto always being there, and i just think thats bad practice anyway.

Also, i know there are MANY other ways of doing this (setting a boolean, or using another var to indicate etc, but i really just want to work with count of items in the array because "business logic"..

EDIT: After doing a little debugging, i'm seeing that i have an array of "Object, undefined". How is this even possible :)

enter image description here

Some search lead me to this. Why are some values ​​in my array undefined

EDIT: Seems that using a delete may cause this problem

Community
  • 1
  • 1
CarComp
  • 1,929
  • 1
  • 21
  • 47
  • where is your code???? – Sudharsan S Jun 30 '15 at 14:37
  • I didn't think this really required me to create code that just makes an angular scope object array. Any created have this 'proto' object appended to them. – CarComp Jun 30 '15 at 14:39
  • 2
    That `proto` is probably the internal [[Prototype]]. It doesn't increment the length. The length must be incremented at some other place. – Oriol Jun 30 '15 at 14:44
  • proto is not included in the length. You can verify this by a simple test: `console.log({foo: [{}]})` – Brian Glaz Jun 30 '15 at 14:45
  • I'm starting to see this. http://plnkr.co/edit/HHTgTHbezdmQrnDkpS30 – CarComp Jun 30 '15 at 14:51
  • Wow i'm an idiot. I found i was using 'delete' on an array index, like it was a property. This will cause this behaviour. Sorry bout this. Vote to close this? – CarComp Jun 30 '15 at 15:06
  • possible duplicate of [Why are some values ​​in my array undefined](http://stackoverflow.com/questions/19807953/why-are-some-values-in-my-array-undefined) – CarComp Jun 30 '15 at 15:07

0 Answers0