0

I'm using Ionic Framework and AngularJS to build a mobile app and I'm trying to the pass the ID of an item from the template into my controller but can't figure out why I'm receiving an undefined value. The value shows up on the template and in the rendered HTML, but when I attempt to call it in the controller it is empty

My template looks like this

<button class="button" ng-click="like({{sale.id}})">Like</button>   
<span>ID: {{sale.id}}</span>

sale.id is showing up on the page and when I inspect the element, it is inside the like function.

On my controller I have:

$scope.like = function(id) {
    console.log(id);
}

The id is showing up as undefined in the console.

3
  • It should simply be like(sale.id), I think. Commented Jul 3, 2015 at 10:06
  • sale.id is $scope variable... pass it directly without parenthesis. Commented Jul 3, 2015 at 10:10
  • I know this is pretty old.. but I'm having a similar issue where I can display a variable within a template, but when I try to pass that same variable to a function, it is always undefined. I've tried with and without string interpolation syntax {{}} Commented Jan 6, 2022 at 20:40

1 Answer 1

1

Just pass sale.id without the {{}} - ng-click is a directive for which you don't have to use {{}} syntax.

Like so:

<button class="button" ng-click="like(sale.id)">Like</button>
Sign up to request clarification or add additional context in comments.

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.