I am using the $rootScope to get and update an avatar. Now, when the user updates a new avatar, the ng-src of the should also update, showing the new avatar that has just been uploaded.
I have realized this by watching the $rootScope.myAvatar in my controller and then setting the $scope.myAvatar to the newValue. Unfortunately, the ng-src is not updating in this case. I have read many questions concerning problems with ng-src and null values, but this is just a change in the $scope and I don't understand why updating $scope.myAvatar does not cause the change of ng-src. I have tried manually calling $scope.$apply(), it does not work as the $rootScope is still digesting.
router.js:
...
.when('/account', {
'templateUrl': 'components/account/account.tpl.html',
'controller': 'accountCtrl as ctrl',
'auth': true,
'reloadOnSearch': false
})
...
Controller as ctrl:
function accountCtrl($scope, $rootScope, accountService) {
var vm = this;
accountService.getAvatar().then(function(res) {
vm.myAvatar = res;
});
$rootScope.$watch('myAvatar', function (newValue, oldValue) {
if (newValue) {
vm.myAvatar = $rootScope.myAvatar;
}
});
}
And the html:
<img ng-src="{{ctrl.myAvatar}}" alt="avatar" width="100" height="100"/>