I had the same problem, I had a <user-picture> directive and on the user settings page the user can change his picture.
The picture comes from an 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.
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. 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. 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. And like that it updates the directive image.
$resourceor$http, you shouldn't need to call$apply.$scope.temimagefilepath=''(empty string)inside the.thencallback. and on the very next line reassigning it the actual image url value path. Also this should be done when usingng-srcon the element