在Angular中,可以使用ngIf和ngFor指令结合正则表达式和CSS来进行条件格式化标签。下面是一种解决方法的示例代码:
{{ item.name }}
.highlight {
color: red;
font-weight: bold;
}
export class AppComponent {
items: { name: string }[];
constructor() {
this.items = [
{ name: 'example' },
{ name: 'another_example' },
{ name: 'not_match' }
];
}
}
在上述代码中,ngFor指令用于循环遍历items数组中的每个元素。ngIf指令配合正则表达式判断item.name是否满足条件,如果满足则显示相应的标签。ngClass指令根据条件判断是否添加highlight类,从而实现条件格式化标签。