1

I'm trying to dynamically generate an HTML link and have that link be the href attribute of my anchor tag inside my AngularJS application. I have the code below:

const objectUrl = baseUrl + s3Bucket + '/' + objectKey;
const link = '<a href=`${objectUrl}`> MyLink</a>';

I thought that using JS's string templating would work but this inserts %60$%7BobjectUrl%7D%60 at the end of URL. I've also tried

const link = '<a href={{objectUrl}}> MyLink</a>';

but that gives me the same literal string %60$%7BobjectUrl%7D%60. Is there any way to insert the variable so that the href link becomes baseUrl + s3Bucket + '/' + objectKey?

1
  • How do you insert link in your template? Why don't you write the acncor tag in your template and use ng-href="{{objectUrl}}" on it? Commented Jun 20, 2019 at 6:33

4 Answers 4

2

try this

const link = '<a href="'+objectUrl+'" > MyLink</a>';
Sign up to request clarification or add additional context in comments.

Comments

1

You have the template string the other way around. The ` should go on the outside of your string.

const objectUrl = 'yourURL';
const link = `<a href="${objectUrl}">MyLink</a>`;

console.log(link);

Comments

0

Try

<a ng-attr-href="{{objectUrl}}">My Link</a>

Comments

0

Use template literal like this:

const link = `<a href="${objectUrl}">MyLink</a>`

Comments

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.