angular - 绑定到Angular 12中div的未知属性
问题描述
我试图通过将属性绑定到组件中的属性来切换属性的值。
<div class="app-container" [data-theme]="theme">
<router-outlet></router-outlet>
</div>
import { Component } from '@angular/core';
import { ThemeService } from './common/_services';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
theme = 'dark';
constructor(private themeService: ThemeService) {
this.themeService.isDark.subscribe(x => {
this.theme = x ? 'dark' : 'light';
});
}
}
通过设置dark
或属性,使用不同light
的data-theme
颜色。所以我只需要一种切换值的方法。
如果我尝试使用上面的代码,我会得到:
错误:src/app/app.component.html:1:28 - 错误 NG8002:无法绑定到“数据主题”,因为它不是“div”的已知属性。
有什么方法可以轻松地将我的属性绑定到模板?为什么我会收到这个错误?
解决方案
我只需要使用属性绑定,如下所示:
[attr.data-theme]="theme"
推荐阅读
- open-liberty - Open Liberty 入门:404
- html - 使用 snapchat 浏览器下载 HTML 文件失败
- javascript - 硬刮 API
- python - 在 pyQt4 中 QCheckBox 的 clicked() 和 stateChanged() 信号有什么区别?
- java - 让 Java Consumer 接受 Kotlin lambda
- python - 当使用 sympy 的积分有多个解时如何检索特定解?
- python - Google Sheets API Python 批量更新和插入注释
- python - 在 matplotlib 上缩放两个不同的 y 轴的问题
- excel - 组合多个宏 (worksheet_change)
- matlab - 带有变量名称的 for 循环 MATLAB