简体   繁体   English

如何使用 Angularjs 中的锚元素 href 链接将 append 变量

[英]How to append variable with anchor element href link in Angularjs

How can I append the variable to the href link?我如何将 append 变量指向 href 链接? I tried something like -我试过类似的东西 -

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

This is not working.这是行不通的。

Look into Template Literals , it helps making strings more understandable查看Template Literals ,它有助于使字符串更易于理解

Try this one:试试这个:

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

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.此外,当您使用箭头函数时,您可以省略return语句,但前提是您避免使用大括号。 Your function wasn't returning anything您的 function 没有返回任何内容

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

Here's a sample in plain JS这是纯 JS 中的示例

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM