11

I have a json which has spaces in key names.

The JSON has the following format

    {
     "response":{"docs":[
        {
          "my name":"krammer",
          "job": "stackexchange"
        }
                }
    }

While using ng-repeat to get the parameters into a list, I use the following code

{{friends.['my name']}}

But this gives an empty output. While

friends.my name

gives an error.

So how can the key names with empty spaces be accessed using AngularJS ?

3
  • {{friends['my name']}}?? Commented Feb 26, 2014 at 11:21
  • post your ng-repeat as well plz Commented Feb 26, 2014 at 11:23
  • 1
    @SajithNair you should have posted it as an answer, because it's obviously the issue. Commented Feb 26, 2014 at 11:26

4 Answers 4

32

Please try this

{{friends['my name']}}
Sign up to request clarification or add additional context in comments.

Comments

4

It doesn't have anything to do with angular, this is the way we read props from JavaScript object, here you have a object called friends. so these are all we can do, if we don't have invalid javascript characters for naming in JavaScript, like space and some other:

friends.myname
friends['myname']
friends["myname"]

but when we have those invalid characters we only can do:

friends['my name']
friends["my name"]

Comments

0

You may run into a case where {{friends['my name']}} does not work. If so, try the following:

{{friends.my_name}}

Comments

-1

{{friends['my name']}} will be working fine

1 Comment

This is exactly the same answer as the accepted one posted two years ago. What extra value does this answer provide?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.