1

I have a $scope variable scopeVar which contains a JSON Object. The JSON object has some complicated key names such as "onStatus[1]".

Is it possible to resolve such key names inside the view template so that I can use them like {{scopeVar.onStatus[1]}} or ng-bind="scopeVar.onStatus[1]" ?.

PS- I assume that using JSON keys in such a fashion is possible after reading this answer.However I am still skeptical regarding using symbols like '[' etc in key names as they may also be used to represent array elements.

Community
  • 1
  • 1
Abdul23
  • 5,265
  • 2
  • 16
  • 23

2 Answers2

1

If onStatus[1] is actually a property name and not the second element of the onStatus array you should use bracket notation to access the property:

{{ scopeVar['onStatus[1]'] }}

or as the expression in ngBind:

ng-bind="scopeVar['onStatus[1]']"
dfsq
  • 191,768
  • 25
  • 236
  • 258
  • Yes. Thats how it should work. Actually I tried this solution but used it as `{{scopeVar[onStatus[1]]}}` . Didn't put the key name as a string inside '..'. Thanks for the early reply. – Abdul23 Dec 29 '14 at 07:55
1

Use it like this in the view it works for me.

{{ scopeVar['onStatus[1]'] }}

Basically in the interpolation anything you put is treated as plain JS code so anything that works in the console of your browser will also work in the between the curly braces.

Ashish Singh
  • 739
  • 3
  • 8
  • 21