如何在 Angularjs 中使用锚元素 href link 附加变量

How to append variable with anchor element href link in Angularjs

如何将变量附加到 href link? 我试过类似 -

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

这不起作用。

查看 Template Literals,它有助于使字符串更易于理解

试试这个:

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

使用模板文字将有助于提高可读性,但将反引号放在整个字符串的外面。此外,当您使用箭头函数时,您 可以 省略 return 语句,但前提是您避免使用大括号。你的函数没有返回任何东西

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

这是一个纯 JS 示例

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

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