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

3

6

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?

Elsaelsbeth answered 8/3, 2016 at 6:39 Comment(2)
try this <a ng-click="switchLanguage(language.value)" >Translate</a>Miele
for those interested in angular (not js), the syntax is (click)="switchLanguage(lang.value)"Poundage
B
7

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

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

Brumaire answered 8/3, 2016 at 6:40 Comment(0)
D
4

Instead of

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

Use this

<a ng-click="switchLanguage(language.value)" >Translate</a>
Defaulter answered 8/3, 2016 at 6:47 Comment(0)
B
3

You can pass the value in this way :

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

This <a ng-click="switchLanguage('{{language.value}}')" >Translate</a> will pass the '{{language.value}}' as a value.

Bala answered 8/3, 2016 at 6:58 Comment(1)
how can we pass like this <a (click)="language.value">Translate</a> language.value is dynamic is it possible?Lippe

© 2022 - 2024 — McMap. All rights reserved.