0

My AngularJs app reads a json file having key/value pairs. The key/value pairs are generated by a java program using HashMap.

I am trying to generate HTML using ng-repeat directive in AngularJs in the following manner.

<li ng-repeat="(line, lineContents) in errorsFromLogFile">
    {{line}} - {{lineContents}} 
</li> 

I also want to access the previous (key / value) pair.

Gone through these stackoverflow links How to obtain previous item in ng-repeat, Compare values inside ng-repeat and tried to access the previous key as

{{ errorsFromLogFile[$index-1].line }} 

but it gives nothing. How to access previous key/value in this case?

The json file looks like this

- errorsFromLogFile: {
     7308: "/tmp/cct7oRJm.s:2099392: Warning: .stabs: description field '11668' too big, try a different debug format",
     7309: "/tmp/cct7oRJm.s:2099393: Warning: .stabs: description field '120d3' too big, try a different debug format",
     7310: "/tmp/cct7oRJm.s:2099394: Warning: .stabs: description field '128a6' too big, try a different debug format",
     7311: "/tmp/cct7oRJm.s:2099395: Warning: .stabs: description field '13046' too big, try a different debug format",
     7312: "/tmp/cct7oRJm.s:2099396: Warning: .stabs: description field '1386c' too big, try a different debug format",
   }
Community
  • 1
  • 1
r_k
  • 77
  • 1
  • 8

1 Answers1

1

You may have to use internal property $$prevSibling which gives the previous sibling scope and in this case previous scope created by ng-repeat. This is because you are using an object map and not Array. errorsFromLogFile[$index-1] will not work in array of objects because it just means that you are trying to access value of property index from the array, i.e errorsFromLogFile[0], errorsFromLogFile[1] etc.

i.e {{$$prevSibling.line}} and {{$$prevSibling.lineContents}} which should work, but is not advisable to use private properties on scope.

Another better suggestion is to convert it to array of objects and just have regular ng-repeat iteration syntax and get the previous item using the index.

PSL
  • 123,204
  • 21
  • 253
  • 243
  • Thanks. This solves the issue. Since I am using object map not Array, and accessing keys in a serial fashion is not possible [Access first property of an object](http://stackoverflow.com/questions/983267/access-the-first-property-of-an-object). Now I get all the keys _(lines)_ from the object _(errorsFromLogFile)_ and use **ng-repeat** over it. – r_k Aug 28 '15 at 12:03