Pass the dynamic value of a function to angular using ng-click

I am 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 at runtime it is populated with the proper value ('en-us', 'ja-jp', etc ...)

And here is my function inside the controller:

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

However, every time I click on the link, the console displays the value as {{language.value}} instead of the correct value (for example: en-us).

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

+5
source share
3 answers

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

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

+5
source

Instead

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

Use this

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

You can pass the value as follows:

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

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

+2
source

Source: https://habr.com/ru/post/1244637/


All Articles