Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a dynamic value to a function in angular with ng-click

I'm trying to use a link to dynamically translate a website.

This is my HTML:

<a  ng-click="switchLanguage('{{language.value}}')" >Translate</a>

{{language.value}} is a dynamic value taken from a json file and I can verify that upon runtime, it does get populated with the proper value ('en-us','ja-jp', etc...)

And here's my function inside a controller:

function switchLanguage(newlan) {
    console.log(newlan);
}

However, everytime I click on the link, the console shows the value as {{language.value}}, instead of the proper value (ex: en-us).

How do I make the value inside the ng-click pass the correct parameter to the function?

like image 634
Artvader Avatar asked Mar 08 '16 06:03

Artvader


Video Answer


2 Answers

use ng-click="switchLanguage(language.value)"

Here is the PLUNKER: http://plnkr.co/edit/uOUD9f1P3tKp3IlGsjBK?p=preview

like image 120
Sunil Lama Avatar answered Sep 20 '22 18:09

Sunil Lama


Instead of

<a ng-click="switchLanguage('{{language.value}}')" >Translate</a>

Use this

<a ng-click="switchLanguage(language.value)" >Translate</a>
like image 22
Subodh Ghulaxe Avatar answered Sep 24 '22 18:09

Subodh Ghulaxe