如何在 Angular2+ 的 index.html
中使用 *ngIf 条件。我想使用 *ngIf
根据条件加载标签,因为我在本地存储中有一个值。这是我的代码,但它不起作用。
<head *ngIf="localStorage.getItem('theme_token') === 'site-layout'"></head
当我使用脚本标签时,它在 javascript 中获取主题标记的值。
<script>
var _val = localStorage.getItem('theme_token'); /// This part is working returning me the value.
</script>
Note: I don't want to hide. I have to render it using
*ngIf
condition.
最佳答案
*ngIf 不会为您解决问题。 *ngIf 将在 Angular 内工作。试试这个例子可能会有帮助。它适用于 Angular 8。在您的应用程序组件中。
constructor() {
if (localStorage.getItem('theme_token') === 'site-layout') {
const temp = document.createElement('link');
temp.innerHTML = '<link id="default-css" href="assets/somestyle.css" rel="stylesheet" media="all">'
;
const head = document.head;
while (temp.firstChild) {
head.appendChild(temp.firstChild);
}
}
}
可以根据自己的情况加载。
关于javascript - 如何在 index.html angular 8 中使用 *ngIf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62356887/