如何在值未更改时更新 AngularJS 视图? [英] How to update AngularJS view when the value has not changed?

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

问题描述

在我看来,我从用户的个人资料中展示了一张照片,在我看来,我有:

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.

有什么想法可以强制视图刷新图片,即使模型上的值未更改(但底层数据已更改)?

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...

如果你想像下面提到的@Tom,你也可以将两者结合起来.我不喜欢这样做,因为您可能正在修改可以保存的对象".例如,如果您使用 ng-resource 并且修改了 pathRelative 然后 $save,则会导致 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天全站免登陆