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

use*_*508 6 javascript angularjs angularjs-directive angularjs-scope

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

我想我必须使用$ scope.$ apply()但每次我使用它时都会收到错误消息"正在进行摘要循环"

template.html

 < img src='{{tempimagefilepath}}/> <!--image-->
Run Code Online (Sandbox Code Playgroud)

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();
    });
Run Code Online (Sandbox Code Playgroud)

谢谢

解:

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

Kev*_*one 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"; 
    });
});
Run Code Online (Sandbox Code Playgroud)


小智 2

您可以尝试使用ng-src而不是 src

<img ng-src="{{tempimagefilepath}}" />
Run Code Online (Sandbox Code Playgroud)

我认为你不需要做 $scope.$apply(); 然后