user2989731
user2989731

Reputation: 1359

Variable from angular template shows up as undefined in controller

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.

Upvotes: 0

Views: 389

Answers (1)

callmekatootie
callmekatootie

Reputation: 11228

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>

Upvotes: 1

Related Questions