简体   繁体   English

Angular 4刷新 <img src={{ … }} > 当价值变化时

[英]Angular 4 refresh <img src={{ … }} > when the value changes

I am using Angular 4 and I have a component where I want to change my user current photo.. so the html code that displays the current user photo is this.. 我正在使用Angular 4,我有一个组件,我想更改我的用户当前的照片..所以显示当前用户照片的HTML代码是这个..

<div class="profilphoto">
        <img src= {{currentphoto}}>
</div>

currentphoto contains the current user photo url and I get it from firebase.. After that I display a Gallery of photos so the user can select one and change his profil photo using a form.. the submit code is the following and works fine currentphoto包含当前用户的照片网址,我从firebase获取它。之后,我显示了一个照片库,以便用户可以选择一个并使用表单更改他的个人资料照片..提交代码如下,工作正常

    onSubmit = function(form) {
    this.photoUrl = form.photoUrl;
    firebase.database().ref("users/"+this.authService.cusername+"/photo").update({
      url: form.photoUrl
    }).then(()=>{
      this.currentphoto = this.authService.photourl;
      console.log("currentphoto:"+this.currentphoto);
    }
    );
  }

Everything works fine except that despite the currentphoto value has changed and database url upadated the html still displays the previous user's image and its annoying(if I refresh the page it shows the new profil image).. Is there any way I can make 一切正常,只是不顾currentphoto值已发生变化,数据库URL upadated的HTML仍显示前一个用户的图像和恼人的(如果我刷新页面,它显示了新PROFIL图片)..有什么办法可以使

<div class="profilphoto">
        <img src= {{currentphoto}}>
</div>

detect when currentphoto changes value and Display the image with the new src value?? 检测currentphoto何时更改值并使用新的src值显示图像?

Try calling the changedetection manually, 尝试手动调用changedetection

constructor(private cdRef: ChangeDetectorRef){

}

Once you set the image 设置图像后

 this.currentphoto = this.authService.photourl;
 this.cdRef.detectChanges();

Without the entire code of your component/service is hard to know what is the problem, but the best shot is in this line: 没有你的组件/服务的整个代码很难知道是什么问题,但最好的镜头是在这一行:

onSubmit = function(form) {

probably it's a problem with the 'this' value. 可能这是'this'值的问题。 Insert a console.log(this) below this line (inside the function) and check if 'this' is a reference to the component instance or to the window. 在此行下方(在函数内)插入一个console.log(this),并检查'​​this'是否是对组件实例或窗口的引用。

Try to use arrow function: 尝试使用箭头功能:

onSubmit = form => {
 //do what you need here, call the service, etc...
 //and then set the this.currentphoto
}

This is probably not relevant anymore but in case it could help people get this solved faster, here it is. 这可能不再相关,但如果它可以帮助人们更快地解决这个问题,这就是它。

If you want to update the image after it has changed write your src like this: 如果你想在更改后更新图像,请写下你的src,如下所示:
src='{{currentphoto}}?d={{clock_tick}}' SRC = '{{currentphoto}} D = {{clock_tick}}'

Initialize your clock tick when you first load your component (I use Date.now()) and again after you update the image. 首次加载组件时(我使用Date.now())并在更新图像后再次初始化时钟标记。 This will tell your browser that you updated the image and it will reload it for you. 这将告诉您的浏览器您更新了图像,它将为您重新加载它。

It worked for me. 它对我有用。

Have you tried this: 你试过这个:

<div class="profilphoto">
        <img [src]="currentphoto" >
</div>

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

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