1

I'm using some data from Google Analytics in my app, which uses colons in the hash keys. For example,

var pages = [{
    'ga:sessions': 100,
    'ga:adImpressions': 1000
}, ...];

But Angular runs into problems when I want to order by something with a colon. This:

<div ng-repeat="page in pages | orderBy:'ga:sessions':true">

Throws this:

Error: [$parse:syntax] Syntax Error: Token ':' is an unexpected
token at column 3 of the expression [ga:sessions] starting at
[:sessions].

EDIT: Here's what I've already tried:

  1. Storing the key in a var and using orderBy:myKey:true
  2. Escaping the colon with \
  3. And in an act of desperation, using the unicode escape code for colon
mz3
  • 1,314
  • 11
  • 27

3 Answers3

2

Use a function which will return a value at the specific key of each object in array:

$scope.filterFunc = function (obj){
    return obj['ga:sessions'];
}

And in the HTML:

<div ng-repeat="page in pages | orderBy:filterFunc:true">

See also this SO post.

Community
  • 1
  • 1
Shimon Rachlenko
  • 5,469
  • 40
  • 51
0

You can't use it this way because angular parser will not allow you.

You can write your own filter or fix keys before converting them to object.

UPD: Technically it's not valid attribute name according to ECMAScript 5.1 https://es5.github.io/#x7.6

syabro
  • 1,857
  • 2
  • 15
  • 30
0

Im pretty sure escaping the character should work, I found a jsfiddle where they use the escape character() in front of the colon as an example

http://jsfiddle.net/CBgcP/664/

 <li ng-repeat="friend in friends | weDontLike:'Adam\:Adamson'">

Edit: I see that you have tried this already but it seems to work in the jsfiddle. I'll see if I cant find another solution.

Chad Watkins
  • 531
  • 1
  • 8
  • 23