简体   繁体   English

如何用角度刷新图像

[英]How to refresh an image with angular

I need to refresh an image that I load like this:我需要刷新我这样加载的图像:

 <img [src]="linkPicture" alt="profile photo" width="150px" height="150px">

Into the typescript file I have a variable this.linkPicture that I store initially the link.在打字稿文件中,我有一个变量 this.linkPicture ,我最初存储链接。 When I update the picture don't refresh.当我更新图片时不要刷新。 I set the variable again....我再次设置变量....

Here is part of my typescript code这是我的打字稿代码的一部分

public linkPicture          : string;

Set the variable initially初始设置变量

this.service.GetProfile(id).subscribe(resp => {
        .....
        this.linkPicture = resp.Photo;
        console.log(resp); //this show the photo
    });

When I update the picture当我更新图片

let dialogRef = this.dialog.open(DialogUploadPhotoComponent, {
        data: {
            patientID: this.ss.getCurrentUserApi().PatientID
        },
        width : "550px",
        });
        dialogRef.afterClosed().subscribe(result => {
            if(result != undefined){
                console.log(result);
                this.linkPicture = result;   //I set the new picture.                   
            }
        });

You can append a timestamp query to the image URL to force the browser to reload the image.您可以将时间戳查询附加到图像 URL 以强制浏览器重新加载图像。 This shouldn't have any negative side effects with the server hosting the image.这应该不会对托管图像的服务器产生任何负面影响。

Update the template to call a function:更新模板以调用函数:

<img [src]="getLinkPicture()" alt="profile photo" width="150px" height="150px">

Compute the URL using an optional timeStamp value:使用可选的时间戳值计算 URL:

public getLinkPicture() {
     if(this.timeStamp) {
        return this.linkPicture + '?' + this.timeStamp;
     }
     return this.linkPicture;
}

When the URL is changed.当 URL 更改时。 Create a new timeStamp value.创建一个新的时间戳值。

public setLinkPicture(url: string) {
     this.linkPicture = url;
     this.timeStamp = (new Date()).getTime();
}

Even if the URL is the same the timestamp changes, and this forces the browser to reload the image.即使 URL 相同,时间戳也会更改,这会强制浏览器重新加载图像。

  backgroundImageGenerate()
    {
        var myVar = setInterval(() => {
        this.imageApi();
                   
        console.log(this.isStatusCode);

        if (this.isStatusCode.indexOf(true) - 1) {
            clearInterval(myVar);
        }
        }, 15000)
    }


generateImage(){
        this.campaignsService.generateImage(this.params.id).subscribe((response: any) => {
            this.backgroundImageGenerate();
        }, (error) => {
            this.notify.error('Something went wrong');
            console.log(error)
        })
    }
<button type="button" (click)="generateImage()" mat-raised-button color="primary"
      [title]="'ADD_CAMPAIGN_PRODUCT' | translate:lang" style="margin:5px;">
      <i class="material-icons">add_circle</i>{{ 'GENERATE_IMAGE' | translate:lang }}
</button>

you can try like this.你可以这样试试。 at least I solve liked this至少我解决了喜欢这个

on your .ts file在你的 .ts 文件上

export class MainLayoutComponent implements OnInit {
public isDesktopDevice: boolean = true;
public sidebarBackgroundClass!: string;


public sideBarOpen: boolean = true; // *sidebar is open by default
}

ngOnInit(): void {
if (this.isDesktopDevice) { this.sidebarBackgroundClass = this.getLayoutBackground(); 
}

public getLayoutBackground(): string {
const bgs: string[] = [
  'side1-1',
  'side1-2',
  'side2-1',
  'side2-2',
];

const max = bgs.length;
const min = 0;
const index = Math.floor(Math.random() * (max - min)) + min;
const bgClass = bgs[index];
return bgClass;

then add photos to your css file one by one like this:然后像这样一张一张地将照片添加到你的css文件中:

.css .css

.side1-1{ background-image: linear-gradient(rgba(0, 0, 0, 0.2), rgba(0, 0, 0, 0.2)),url('/assets/images/side/side1.1.jpg') !important;}

the final add class to your html:最后将类添加到您的 html:

<mat-drawer [class]="sidebarBackgroundClass"></mat-drawer> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM