当模型改变时,angularjs图像src会发生变化

时间:2013-10-11 04:23:12

标签: javascript angularjs angularjs-directive angularjs-scope

我正在服务器上旋转图像,我想知道如何在我的页面上显示图像更改?

我想我必须使用$ scope。$ apply()但每次我使用它时都会收到错误消息“digest cycle in progress”

template.html

 < img src='{{tempimagefilepath}}/> <!--image-->

controller.js

 photoalbumServ.rotate_photo(post).then(function(data) {
  //after server modifies photo
    $scope.tempimagefilepath = $scope.baseurl + "user_images/user_43/temp/123-temp.jpg";
     $scope.$apply();
    });

感谢

解决方案:

我的解决方案是更改范围值{{tempimagefilepath}},以便更改图像。这要求我在旋转图像时不断重命名服务器上的文件。

5 个答案:

答案 0 :(得分:7)

两件事。首先,您应该使用ng-src而不是src来阻止您的客户在角度评估表达式之前尝试加载图片。

其次,传递$apply()一个函数回调,它会进行必要的范围更改:

photoalbumServ.rotate_photo(post).then(function(data) {
    //after server modifies photo
    $scope.$apply(function() {
        $scope.tempimagefilepath = $scope.baseurl + "user_images/user_43/temp/123-temp.jpg"; 
    });
});

答案 1 :(得分:4)

我认为这是因为您的浏览器缓存。不需要$ apply()

试试这个

var random = (new Date()).toString();
$scope.tempimagefilepath = newUrl + "?cb=" + random;

答案 2 :(得分:2)

您可以尝试使用ng-src代替src

<img ng-src="{{tempimagefilepath}}" />

我认为你不需要做范围。$ apply();然后

答案 3 :(得分:0)

Template HTML 
 < img ng-src='{{tempimagefilepath}}/>

AngularJS Code
photoalbumServ.rotate_photo(post).then(function(data) {
    //after server modifies photo
    $scope.$apply(function() {
        $scope.tempimagefilepath = $scope.baseurl + "user_images/user_43/temp/123-temp.jpg"; 
    });
});

答案 4 :(得分:0)

使用ng-src,就像其他人所说的那样。但是,请务必参考您的控制器

[[0. 2. 2.]
 [2. 0. 1.]
 [2. 1. 0.]]
相关问题