在Angular中使用ng-src无法更新图像
image not updating using ng-src in Angular
在我的控制器中,我调用一个服务。这个服务向一个PHP文件发出请求,该文件给我一个指向图像的URL。
这样做的原因是图像位于"Media Vault"中,这意味着我需要生成一个散列URL,以便查看它:
http://xxxx.vo.llnwd.net/o35/s/xxx/test/thumbs/slide1.jpg?e=1365006137&h=5852aeaf5beeeebff05a601801b61568
上面的URL已经过期,所以你不会看到来自特定链接的任何内容。
我的图像标签是这样的:
<img id="thumbnail" class="glow" ng-src="{{currentThumb}}" width="200" height="150"/>
currentThumb在我的控制器是:
$scope.currentThumb = ImageService.getImage({
'type' : 'thumb',
'index' : $scope.currentIndex + 1,
'location' : $scope.location
});
和我的服务看起来像这样:
app.factory('ImageService', function($http)
{
var image = null;
return {
promise:null,
getImage: function($data)
{
this.promise = $http.post('resources/auth.php',$data, {timeout:20000}).success(function(response)
{
image = response;
log(response);
})
}
}
});
当前的ImageService成功返回一个URL(至少它在控制台中显示了URL),当单击控制台中的日志结果时,图像加载正常。为什么图像没有更新?
我不确定你需要如何调用这个,但另一种可能更好的方法是在控制器的解析块中调用ImageService.getImage()
。这样,在控制器和视图被处理之前,它就已经加载了。
jsfiddle
http://jsfiddle.net/HYDmj/1/<
视图/strong>
<div ng-app="main">
<div ng-controller="ExampleController">
<h1>currentThumb</h1>
<img ng-src="{{currentThumb.data}}">
</div>
</div>
var app = angular.module('main',[]);
app.factory('ImageService', function($http)
{
image = null;
return {
getImage: function($data)
{
image = $http.post('http://dopserv1.dop.com/test/auth.php', $data, {timeout:20000})
return image
}
}
});
function ExampleController($scope, ImageService)
{
$scope.currentThumb = ImageService.getImage({
'type' : 'thumb',
'index' : 1,
'location' : 'http://digitalout.vo.llnwd.net/o35/s/mobilevforum/test/'
});
}
相关文章:
- 如何使用更新图像源以响应新闻事件
- 更新图像切换上的php会话变量
- Flexslider-通过ng repeat更新图像-宽度不正确
- 将多个输入值传递给函数以更新图像
- 将多个选择值传递给函数以更新图像
- 有没有更好的方法可以用jquery更新图像的高度和宽度
- 如何让javascript执行以更新图像
- 从AJAX成功函数更新图像源
- 更新图像 url 时,图像不会在前端呈现
- JavaScript 自动更新图像
- 更新图像时,它不会保存在数据库中
- 自动更新图像加载,无需更改 html 代码
- 输入 keydown,单击IE 7中的事件更新图像
- 更改 src 属性不会更新图像
- j查询更新图像 src 在悬停时
- 在 html 中更新图像上的文本
- 使用jQuery更新图像源
- 谷歌地图:更新图像覆盖
- 使用数据URI快速更新图像会导致缓存、内存泄漏
- 使用AJAX从php页面收集链接并更新图像