【发布时间】:2021-01-22 19:33:13
【问题描述】:
我正在使用一个视频系列来熟悉 Angular,codewithmosh。他在其中一个练习中使用了 glyphicons,但该视频来自 2018 年。从那时起,引导程序似乎停止了对 Glyphicon 的支持,但引导程序站点仍然包含其图标的 html。我想我会看看我是否可以通过推动原始 html 来做同样的事情。
问题是,从 component.ts 中提取的 html 没有呈现,而只是在页面上显示 html 代码。 component.html 中的相同 html 正在呈现。
我真的是新来的,所以我的代码中可能存在明显的缺陷。它可能不漂亮,但听起来不错,我不知道为什么它不起作用。理想情况下,这应该呈现为一个空星,单击它会切换为全星。
提前感谢您的帮助!
这里是 app.component.ts:
import { Component } from '@angular/core';
@Component({
selector: 'app-star',
templateUrl: './star.component.html',
})
export class StarComponent {
isClicked: boolean;
fullStar: string = `
<svg (click)="onClick()" width="2em" height="2em" viewBox="0 0 16 16" class="bi bi-star-fill" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<path d="M3.612 15.443c-.386.198-.824-.149-.746-.592l.83-4.73L.173 6.765c-.329-.314-.158-.888.283-.95l4.898-.696L7.538.792c.197-.39.73-.39.927 0l2.184 4.327 4.898.696c.441.062.612.636.283.95l-3.523 3.356.83 4.73c.078.443-.36.79-.746.592L8 13.187l-4.389 2.256z"/>
</svg>
`;
emptyStar: string = `
<svg (click)="onClick()" width="2em" height="2em" viewBox="0 0 16 16" class="bi bi-star-fill" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<path d="M3.612 15.443c-.386.198-.824-.149-.746-.592l.83-4.73L.173 6.765c-.329-.314-.158-.888.283-.95l4.898-.696L7.538.792c.197-.39.73-.39.927 0l2.184 4.327 4.898.696c.441.062.612.636.283.95l-3.523 3.356.83 4.73c.078.443-.36.79-.746.592L8 13.187l-4.389 2.256z"/>
</svg>
`;
currentSymbol: string = this.emptyStar;
onClick() {
this.isClicked = !this.isClicked;
this.currentSymbol = this.isClicked ? this.fullStar : this.emptyStar;
}
}
这里是star.component.html:
{{currentSymbol}}
-------
<svg (click)="onClick()" width="2em" height="2em" viewBox="0 0 16 16" class="bi bi-star-fill" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<path d="M3.612 15.443c-.386.198-.824-.149-.746-.592l.83-4.73L.173 6.765c-.329-.314-.158-.888.283-.95l4.898-.696L7.538.792c.197-.39.73-.39.927 0l2.184 4.327 4.898.696c.441.062.612.636.283.95l-3.523 3.356.83 4.73c.078.443-.36.79-.746.592L8 13.187l-4.389 2.256z"/>
</svg>
这是 app.component.html,超级基础:
<app-star></app-star>
【问题讨论】: