1

I want to pass object in onClick event. I'm getting alert like [object Object] I need inside data also.

View:

 <div ng-repeat="action in category.Actions">
    <a ng-click=" actionclick(action)"> {{action.Name}}</a>          
   </div>

Controller:

  $scope.actionclick = function (action) {
               alert("Option Name is " + action);
           };
5
  • Yes, but I'm getting alert like "[object Object]" I need inside data also. Commented Nov 17, 2014 at 18:24
  • alert is still alive? Commented Nov 17, 2014 at 18:24
  • 1
    you get [object Object] because you convert action to string. Commented Nov 17, 2014 at 18:26
  • What do mean by Alive? I'm seeing alert. Commented Nov 17, 2014 at 18:26
  • 1
    @user3194721 put this before all your scripts: window.alert = function(){ console.log.apply(console,arguments) }; Commented Nov 17, 2014 at 18:45

1 Answer 1

2

Every object in javascript outputs [object Object] when coerced into a string unless it's prototype overrides the native toString method.

the plus operator coerce all objects into strings if one of it's operands is of type string.

so these are equal:

("Option Name is " + action) === ("Option Name is " + action.toString())

To see the object itself use the console (developer tools):

console.log(action)

What you probably wanted to do is to reference the name property inside the object:

"Option Name is " + action.name
Sign up to request clarification or add additional context in comments.

1 Comment

in addition, I think the intention was to have ng-click="actionclick(action.name)" instead of just action.

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.