我正在嘗試將 src 添加到我的 html 檔案中的腳本中,但前提是條件為真。否則它不應該運行腳本 src。到目前為止,以下是我的代碼:
<script>
this.booleanValue = JSON.parse(
sessionStorage.getItem("booleanValue")
);
if (this.booleanValue == "true") {
console.log("true");
}
</script>
<script
*ngIf="this.booleanValue != true"
src="code.js"
async
></script>
它控制臺將 booleanValue 正確地記錄為 true,但它仍然運行腳本 src。它不應該,因為它應該只在 false 時運行。有誰知道我做錯了什么?
提前致謝
uj5u.com熱心網友回復:
執行以下操作時,您正在 JSON 決議“booleanValue”:
JSON.parse(
sessionStorage.getItem("booleanValue")
)
并且您嘗試直接獲取 this.booleanValue 的值,在驗證其值是否為真時,它會類似于“this.booleanValue.value”,或者您不使用 JSON.parse()。
示例:如果 Session 變數為 booleanValue => true,則使用 if(this.booleanValue),但如果 Session 變數為 booleanValue => "{value:true}" 或 "{\"value\":true}",那么您使用 JSON.parse 和 if(this.booleanValue.value)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/453414.html
標籤:javascript html if 语句 ng-容器
