【问题标题】:Display thumbnail that user clicks as main image with Angular2使用 Angular2 显示用户单击的缩略图作为主图像
【发布时间】:2017-10-10 03:59:09
【问题描述】:

我正在制作产品缩略图库,并希望将用户单击的缩略图显示为主图像。我正在使用 Angular 执行此操作,但我对框架相当陌生。

product.html

<img src="{{mainImage}}">

<div class="thumbnails">        
        <img src="{{image1}}" (click)="changeMainImg()">
        <img src="{{image2}}" (click)="changeMainImg()">
</div>

product.ts

public mainImage;

public image1 = "http://via.placeholder.com/100x100";
public image2 = "http://via.placeholder.com/100x200";

public changeMainImg(event){
    this.mainImage = "http://via.placeholder.com/100x100";
}

部分

this.mainImage = "http://via.placeholder.com/100x100";

我希望 this.mainImage 等于 image1 或 image2,具体取决于所选的缩略图。

【问题讨论】:

    标签: javascript html angular typescript


    【解决方案1】:

    只需将选定的图像传递给函数并分配它

    <div class="thumbnails">        
            <img src="{{image1}}" (click)="changeMainImg(image1)">
            <img src="{{image2}}" (click)="changeMainImg(image2)">
    </div>
    

    在 TS 中

    changeMainImg(image:any){
        this.mainImage = image;
    }
    

    DEMO

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-11
      • 2010-09-06
      • 2014-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-11
      相关资源
      最近更新 更多