首页 > 解决方案 > 如何在 HTML 属性中使用条件?

问题描述

当用户单击 Angular 中的按钮时,我正在新窗口中加载图像。我打开的弹出窗口包含 HTML。

角HTML:

<button class="btn btn-primary" type="button" (click)="openDocument(doc)"> View </button>

TS:

openDocument(doc) {
    doc.uploadedDocUrl = "https://beta.lottoweaver.com/WeaverDoc/commonContent/www.nationallottery.co.za/playerDocument/408441_ID_PROOF_null_1616082462069.pdf"

    let str = `<embed src=${doc.uploadedDocUrl} width='100%' height="100%">`;
    
    this.OpenWindow = window.open('', '', 'width=900, height=600, left=200, top=100');
    this.OpenWindow.document.write(`<body onload="checkIfPdf(${doc.uploadedDocUrl})">${str}</body>
    <script>
    function checkIfPdf(doc) {
      if(doc.endsWith(".pdf")) {
        console.log("is pdf")
      } else {
        console.log("is not pdf")
     }
    }
    </script>
    `);
    this.OpenWindow.document.close();
  }

我希望将宽度和高度属性保留为100%doc 文件以“.pdf”结尾,否则我希望将宽度和高度属性保留为auto.

这个条件怎么写?

工作 stackblitz= https://stackblitz.com/edit/angular-ivy-ns536z

标签: javascripthtmlangular

解决方案


我不知道角度但是。你的代码就像

 <script>
   function checkIfPdf(doc) {
   if(doc.endsWith(".pdf")) {
   console.log("is pdf")
   } else {
   console.log("is not pdf")
   }
   }
   </script>
  `);
   this.OpenWindow.document.close();
you may check with 

   <script>
  function checkIfPdf(doc) {
   if(doc.endsWith(".pdf")) {
   console.log("is pdf")
   } else {
   console.log("is not pdf")
   }
   }
  `);
   this.OpenWindow.document.close();
  </script>

推荐阅读