1

I'm reverse geocoding when a user clicks on a point on a google map like so:

geocoder.geocode({'location': latlng}, function(results, status) {
            if (status === google.maps.GeocoderStatus.OK) {
                if (results[1]) {
                console.log(results[1]);

At this stage the console log for results[1] looks like

Object {address_components: Array[4], formatted_address: "Rathmines, Co. Dublin, Ireland", geometry: Object, place_id: "ChIJN6MDC6kOZ0gRIhArCabX9o4", types: Array[2]}

But then I try and pass that object in to an ng-click function when the user taps on the infowindow with the address that I have popping up, like so:

ng-click='addressPicked("+results[1]+")'

I get the error

Error: [$parse:syntax] Syntax Error: Token 'Object' is unexpected, expecting []] 
at column 23 of the expression [addressPicked([object Object])] starting at [Object])].

I have this working in another application, but I am just passing through a lat & long string.

Do I need to convert the object to something else before trying to pass it through as a parameter?

Anyone come across this error before with ng-click?

Any help would be much appreciated, thanks.

1
  • 1
    if results variable in scope then do try ng-click='addressPicked(results[1])' Commented Apr 5, 2016 at 21:09

4 Answers 4

9

Without looking at the full code, my guess is that you are getting the error because

ng-click='addressPicked("+results[1]+")'

is converting results[1] to its string representation [object Object] and hence the error.

Try using without inner quotes

ng-click='addressPicked(results[1])'
Sign up to request clarification or add additional context in comments.

Comments

2

You should do this:

ng-click='addressPicked('+results[1]+')'

Because result is a variable (object) must be passed without quotation marks.

Comments

2

For people trying to pass an object through to the next function neither of the above answers work, exactly. In that case you need to use:

ng-click='addressPicked(' + JSON.stringify(object) + ')'

Comments

0
<div class="class1" id="id1" data-ng-click="click1(arg)" data-ng-class="{class2: expression, class3: expression, class4: expression }"> 

I was getting the similar error when my expression was like above and when I have changed the expression like below the issue got resolved. Adding the single quotes ('') across all the classes for ng-class.

<div class="class1" id="id1" data-ng-click="click1(arg)" data-ng-class="{ 'class2': expression, 'class3': expression, 'class4': expression }">

Comments

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.