I'm using a UI.Bootstrap accordion and I've defined my heading like so:
<accordion-group ng=repeat="(cname, stations) in byClient">
<accordion-heading>
{{ cname }} <span class="pull-right"> {{ Object.keys(stations).length }} Stations</span>
</accordion-heading>
When that displays the Object.keys(stations).length
resolves to nothing. If I put that same length call in my controller I get back the expected count. Is there something preventing the method call from working in AngularJS?
The rest of the accordion that uses stations
acts as expected, so I know that it's being populated properly. The byClient
data structure basically looks like so:
{
"Client Name" : {
"Station Name": [
{...},
{...}
]
}
}
In case someone searches for angular 2 and higher solution. It now hat keyvalue pipe, which can be used to interate over objects
Yes, That is because
Object
is a part ofwindow/global
and angular cannot evaluate that expression against the scope. When you specifyObject.keys
in your binding angular tries to evaluate it against the$scope
and it does not find it. You could store the reference ofobject.keys
in some utility in rootScope and use it anywhere in the app.Something like this:-
and use this as:-
Well this will be available to any child scopes except for isolated scopes. If you are planning to do it on the isolated scope (eg:- Isolated scoped directives) you would need to add the reference of
Object.keys
on the scope, or as you expose a method on the scope which will return the length.Or better yet , create a format filter to return the keylength and use it everywhere.
and do:-
Demo
Use the function to determine the number of object properties:
and use:
I think filters are the most AngularJS way of handling structures in template code: