要使用三元运算符来设置img标签的src属性,可以按照以下步骤进行操作:
以下是一个示例:
在组件的ts文件中:
import { Component } from '@angular/core';
@Component({
selector: 'app-image',
templateUrl: './image.component.html',
styleUrls: ['./image.component.css']
})
export class ImageComponent {
imageUrl: string;
constructor() {
// 根据条件设置imageUrl的值
this.imageUrl = condition ? 'path/to/image1.jpg' : 'path/to/image2.jpg';
}
}
在组件的HTML文件中:
请注意,上述示例中的condition是一个根据业务逻辑设置的条件。根据条件的不同,可以设置不同的图片路径。
通过以上步骤,你可以使用三元运算符来设置img标签的src属性,并且根据条件加载不同的图片。