在Angular 2中使用CSS属性选择器的示例代码如下:
[appHighlight] {
background-color: yellow;
}
这是一个带有属性选择器的段落。
Component
装饰器,并将其styleUrls
属性设置为包含CSS文件路径的数组,例如:import { Component } from '@angular/core';
@Component({
selector: 'app-example',
templateUrl: './example.component.html',
styleUrls: ['./example.component.css']
})
export class ExampleComponent { }
以上代码示例了如何在Angular 2中使用CSS属性选择器。在这个示例中,我们定义了一个名为appHighlight
的属性选择器,并将其应用到一个段落元素上。当使用该属性选择器时,该段落元素将具有黄色的背景色。
确保将示例代码添加到正确的组件文件中,并在运行应用程序时确保正确加载CSS文件。