Home > Software engineering >  How to append variable with anchor element href link in Angularjs
How to append variable with anchor element href link in Angularjs

Time:03-26

How can I append the variable to the href link ? I tried something like -

 $scope.downloadFile=(fileName,id)=>{
 <a  href='https://downloadFile?fileName=' fileName '&id=' encodeURIComponent(id)></a>;
 }

This is not working.

CodePudding user response:

Look into Template Literals, it helps making strings more understandable

Try this one:

 $scope.downloadFile=(fileName,id)=>{
 <a  href=`https://downloadFile?fileName=${fileName}&id=${encodeURIComponent(id)}`></a>;
 }

CodePudding user response:

Using template literals will help make it more readable, but put the backticks on the outside of the whole string. Additionally, when you use arrow functions you can omit the return statement, but only if you avoid using curly braces. Your function wasn't returning anything

$scope.downloadFile = (fileName,id) => `<a href="https://downloadFile?fileName=${fileName}&id=${encodeURIComponent(id)}"></a>`;

Here's a sample in plain JS

const downloadFile = (fileName,id) => `<a href="https://downloadFile?fileName=${fileName}&id=${encodeURIComponent(id)}"></a>`

console.log(downloadFile("THEFILE","THEID"));

  • Related