【发布时间】:2022-01-10 02:11:56
【问题描述】:
我的 HTML 中有一个元素,如下所示:
<span [ngStyle]="{'background': getColor(selectedOption.type)}">BLAH</span>
我的 TS 文件中有这样的条件:
public getColor(type: string){
switch (type) {
case 'General':
return {background: '#ffe5d7', color: '#e7560a' };
case 'Interview':
return { background: '#ffe5d7', color: '#e7560a' };
//more code
基本上,如果用户选择“常规”,我希望从函数返回正确的背景和字体颜色,并使用 ngStyle 将其应用于元素。但这不能正常工作。我做错了什么?
【问题讨论】:
-
尝试 BLAH 或从您的退货声明中删除背景。因为您要复制背景两次。同样,您还应该从 return 中删除括号