4

在我的控制器中,我调用了一项服务。该服务向 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),并且当单击控制台中的日志结果时,图像加载正常。图像不更新的任何原因?

4

1 回答 1

2

我不确定您需要如何调用它,但另一种可能更好的方法是ImageService.getImage()在控制器的解析块中调用它。这样它就在控制器和视图处理之前全部加载。

jsfiddle

http://jsfiddle.net/HYDmj/1/

看法

<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/'
    });
}
于 2013-04-03T20:26:45.607 回答