UX Guy
UX Guy

Reputation: 257

Why can't I pass an angular variable as a function parameter?

I'm trying to pass the follower through the followPerson() function. This works fine if I do followPerson(123). But when I do followPerson({{follower.follower}}) it doesn't fire.

{{follower.follower}} definitely works as it's shown in bold.

<div ng-repeat="follower in followers.followers">
    <b>{{follower.follower}}</b>
    <a ng-click="followPerson({{follower.follower}})">Follow</a> 
</div>

Upvotes: 0

Views: 644

Answers (1)

PSL
PSL

Reputation: 123739

You must not use interpolation ({{) for the arguments in the ng-click handler, it will just result in a parse error (If not using very old angular versions) due to invalid expression. You just need to pass the argument expression as is, angular will just evaluate is against the scope while evaluating the ng-click handler.

Just do:

 ng-click="followPerson(follower.follower)"

Have a look at your console for errors.

Upvotes: 6

Related Questions