0

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.

4
  • Can you give us any context? Commented Mar 25, 2022 at 10:31
  • Your ' is placed wrong Commented Mar 25, 2022 at 10:32
  • @MaximilianDolbaum where exactly ? Commented Mar 25, 2022 at 10:40
  • @LarsFlieger I have a dropdown and a button to download file selected from the dropdown. For that purpose I need to make a href to take dynamic id and fileName Commented Mar 25, 2022 at 10:42

2 Answers 2

1

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"));

Sign up to request clarification or add additional context in comments.

Comments

1

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>;
 }

2 Comments

This is not working !!
@Amara What is the href in the url?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.