Jose Mendez Jose Mendez - 2 months ago 24
AngularJS Question

angular {{ }} expression not resolving inside ng-click

The line below fails, the console shows "index is undefined" when the image is clicked:

<img class="full-image" ng-click="addFavorite({{dish.id}})" ng-src="{{baseURL+dish.image}}" title="{{dish.name}}" >


However, any of the other expressions resolve the interpolation just fine. I even added this one to prove I could show the dish id:

<h2>{{dish.name}} {{dish.id}}


And I get the dish name and to the right the dish id printed out on the browser.

If I swap the expression to a hardcoded value such as 2, the function addFavorite() runs fine on click:

<img class="full-image" ng-click="addFavorite(2)" ng-src="{{baseURL+dish.image}}" title="{{dish.name}}" >


What could be the error that prevents the addFavorite() funcion from resolving the {{dish.id}} expression?

Answer

when you're using like ng-click you don't need to use {{}}

Update :

It is helpful to you https://docs.angularjs.org/api/ng/directive/ngClick

Comments