简体   繁体   English

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

[英]angularjs image src change when model changes

I am rotating an image on the server and I was wondering how to show the image change on my page? 我正在服务器上旋转图像,我想知道如何在我的页面上显示图像更改?

I think I have to use $scope.$apply() but everytime I use it i get the error message "digest cycle in progress" 我想我必须使用$ scope。$ apply()但每次我使用它时都会收到错误消息“正在进行摘要循环”

template.html template.html

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

controller.js 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();
    });

thanks 谢谢

Solution: 解:

My solution was changing the scope value {{tempimagefilepath}} so the image will change. 我的解决方案是更改范围值{{tempimagefilepath}},以便图像更改。 That required me to constantly rename the file on the server when I rotate the image. 这要求我在旋转图像时不断重命名服务器上的文件。

Two things. 两件事情。 First, you should use ng-src rather than src to prevent your clients attempting to load the image before angular has evaluated the expression. 首先,您应该使用ng-src而不是src来防止客户端在angular评估表达式之前尝试加载图像。

Second, you pass $apply() a function callback that makes the necessary scope changes: 其次,传递$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"; 
    });
});

I think this is because your browser cache. 我认为这是因为您的浏览器缓存。 don't need $apply() 不需要$ apply()

try this 尝试这个

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

You can try and use ng-src instead of src 您可以尝试使用ng-src而不是src

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

I don't think you need to do $scope.$apply(); 我认为你不需要做范围。$ apply(); then 然后

I had the same problem, I had a <user-picture> directive and on the user settings page the user can change his picture. 我有同样的问题,我有一个<user-picture>指令,在用户设置页面上,用户可以更改他的图片。 The picture comes from an api like url + token . 图片来自api like url + token When I change the picture from the database I get success and then I need to update the ng-src="{{mainCtrl.picture.src}}" file on every instance of the directive. 当我从数据库中更改图片时,我获得了成功,然后我需要在指令的每个实例上更新ng-src="{{mainCtrl.picture.src}}"文件。

This is the directive: 这是指令:

 angular.module('appApp') .directive('userPicture', function() { return { restrict: 'E', template: '<img class="img-responsive" ng-src="{{mainCtrl.picture.src}}" />', controller: 'UserPictureCtrl', scope: true, replace: true } }) .controller('UserPictureCtrl',['$scope', '$resource', 'HelpersService', '$sessionStorage', function ($scope, $resource, HelpersService, $sessionStorage) { $scope.mainCtrl.picture = {}; $scope.mainCtrl.picture.src = HelpersService.domain + 'user/getPictureAsStream?token=' + $sessionStorage.token; }]); 
I am binding the ng-src="url file address string" from the mainCtrl. 我从mainCtrl绑定ng-src="url file address string" When I am changing the picture on the database I am reassigning the value of the $scope.mainCtrl.picture.src to the same url + token, but I all sow add in an extra &rand url parameter sow the url looks like this: http://<domain>/<serverApi>/user/getPictureAsStream?token=65145d12-f033-41f1-b101-9ed846352284&rand=0.6513215699 , pay attention to the last parameter, &rand=0.6513215699 . 当我更改数据库上的图片时,我将$ scope.mainCtrl.picture.src的值重新分配给相同的url +标记,但是我都播种了一个额外的&rand url参数播种网址如下所示: http://<domain>/<serverApi>/user/getPictureAsStream?token=65145d12-f033-41f1-b101-9ed846352284&rand=0.6513215699 ,注意最后一个参数, &rand=0.6513215699 This is telling to the browser that it is a different file from a different url and makes a get request to the server for it, on the server instead, the extra parameter is ignored sow it gives me the new picture even if it is located in the same location. 这告诉浏览器它是一个与不同url不同的文件并向服务器发出get请求,而在服务器上则忽略了额外的参数,即使它位于同一个地方。 And like that it updates the directive image. 就像它更新指令图像。

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"; 
    });
});

use ng-src as the others said. 像其他人说的那样使用ng-src。 But it is important to refere to your controller 但重要的是要参考你的控制器

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM