javascript - 如何在 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
解决方案
我不知道角度但是。你的代码就像
<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>
推荐阅读
- android-studio - 找不到参数 junit:junit:4.12 的方法 testImplementation()
- typescript - Typescript 动态生成变量名
- javascript - How to use jQuery UI Sortable to intersect properly?
- .net - .Net HttpClient URI 无效:Uri 字符串太长
- html - 获取 HTML 有序列表中元素的前缀
- python - Anaconda3 安装挂在 install.py 上
- c++ - 在 C++ 中实现串联继承
- spotfire - Conditionally scale values in a crosstable according to a category in Spotfire
- javascript - 同时发送图像和 JSON
- git - How can I reset an *entire repo*?