当该值没有变化如何更新AngularJS看法? [英] How to update AngularJS view when the value has not changed?

查看:144
本文介绍了当该值没有变化如何更新AngularJS看法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,我diplay从用户的个人资料照片,在我看来,我有:

In my view I diplay a photo from the user's profile, in my view I have:

 <img src="{{user.photo.pathRelative}}"/><br/>

当用户上传新照片被上传到服务器,在路径中的图像被更新。其结果是,图象的数据被改变,但是,user.photo.pathRelative保持不变。这就是为什么在视图中的图片不会改变的原因。

When user uploads new photo it is uploaded to the server and the picture at the path is updated. The result is that the data of the picture is changed, however, the user.photo.pathRelative stays the same. This is the reason why the picture in the view does not change.

任何想法我怎么可以强制视图刷新画面即使对模型值没有改变(但数据undeneath被改变)?

Any ideas how I can enforce the view to refresh the picture even if the value on the model is not changed (but the data undeneath is changed)?

推荐答案

您需要使用NG-SRC(只是一个供参考)。

You need to be using ng-src (just an FYI).

 <img ng-src="{{user.photo.pathRelative}}{{randomStr}}"/><br/>

然后设置上传后随机字符串。

And then you set random string after the upload.

$scope.randomStr  = '?randomStr=' +  new Date().getTime();

缺点是这不会刷新后工作...

The downside is this will not work after refresh...

您还可以,如果你想要像@汤姆下面提到将二者结合起来。我不喜欢这样做,BC正在修改可能,可以保存一个对象。例如,如果你使用的是NG-资源,您修改pathRelative,然后保存$,这将导致pathRelative在数据库中进行更新。

You could also combine the two if you want like @Tom mentioned below. I do not like doing this, bc you are modifying potentially an "object" that could be saved. For instance if you are using ng-resource and you modified pathRelative and then $save, it would result in the pathRelative being updated in the database.

这篇关于当该值没有变化如何更新AngularJS看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆