Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

bug? when using orderBy filter with an array of objects and a field with spaces #8592

Closed
Loksly opened this issue Aug 13, 2014 · 1 comment
Closed

Comments

@Loksly
Copy link

Loksly commented Aug 13, 2014

Hi everybody.

I get an "Error: [$parse:syntax] Syntax Error" when using a field that has two or more words.

For example for this array of objects:

            [
              { "label": "one",  "value": 1, "sort value": 2,  "sort": 3  },
              { "label": "two",  "value": 2, "sort value": 1,  "sort": 2  },
              { "label": "three",  "value": 3, "sort value": 1,  "sort": 1  },
            ]

I can sort by label, value or sort attributes. But it fails when using "sort value".
I guess it's easier to review using a jsfiddle link with an example, here it is:
http://jsfiddle.net/qWzTb/733/

@pkozlowski-opensource
Copy link
Member

@Loksly I believe it works as designed. When you supply an argument to the orderBy filter it is interpreted as an expression that will be evaluated in the context of a given object. Then the result of this evaluation will be used in the sort comparison. So, the important thing to note here that orderBy argument is an expression and not a field name. It might be confusing since in many cases an expression that is field name will evaluate to field value value, but this is not always the case.

If you want to supply a field name to be used in comparison you can do this by providing a constant expression. In your case it would be value: '"sort value"', a working fiddle: http://jsfiddle.net/stdg8ugy/

So while it works as expected I can see that AngularJS doc is not mentioning this constant expression = field name trick, thus it might be confusing. I'm going to send PR with the docs update later today, unless there is someone doing this before me.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants