Change System Logo

4

I'm creating in a system that I'm developing for our client, a function where it allows the same to upload the logo and cut it, then when it's saved I'm trying to load the image that was uploaded, but it happens I need it to always be the same name and in the same destination and the browser can not update it, for example:

Current image name = logo.png;

After uploading the image name continues logo.png but with another image saved, because if I reload the page the logo is applied as it should, I would like to know if there is any way to do this without reloading the page, follow the code which I use to set the image:

$scope.carregarLogo = function(name){
    if($scope.img_name=== '' && angular.isUndefined(name) || name === ''){
       $scope.img_name = url_sistema + 'web_files/img/logo.png';
       $('#img').attr('src',$scope.img_name);
       console.log('deu');
    }else{
       $scope.img_name = name;
       $('#img').attr('src',$scope.img_name);
       console.log($('#img').attr('src'));
       console.log('deu2');
    }
};
    
asked by anonymous 04.02.2016 / 12:31

2 answers

1

Galera, I found a solution here, so I do not need to reload the page, masss have a single problem, can not be added an image with the name that will always be the same (ex: logo.png) where will be selected this image to set in some, otherwise it will not work, the only way to work is when I create the file by PHP, because there will not need to replace the file that exists there, follow the code if you want to test, feel free:

$scope.carregarLogo = function (name) {
    if ($scope.img_name === '' && angular.isUndefined(name) || name === '') {
        $scope.img_name = url_sistema + 'web_files/img/logo.png';
        $('#img').attr('src', $scope.img_name);
        console.log('deu');
    } else {
        var random = (new Date()).toString();
        $scope.img_name = name + "?date=" + random;
        console.log($scope.img_name);
        $('#img').attr('src', $scope.img_name);
        console.log($('#img').attr('src'));
        console.log('deu2');
    }
};

THANK YOU FOR YOUR HELP, MUCH, A GREAT DAY TO ALL FLW

    
04.02.2016 / 16:11
2

Mixing AngularJS with jQuery inside your controller is never a good idea because the two are frameworks with different purposes. jQuery is for DOM manipulation and AngularJS is for manipulating data with influence on the DOM.

Basically, to solve your problem, you need "Think AngularJS Way" and forget what you know about jQuery, at least in the beginning.

One possible solution to your problem:

$scope.carregarLogo = function(name){
    if($scope.img_name=== '' && angular.isUndefined(name) || name === ''){
       $scope.img_name = url_sistema + 'web_files/img/logo.png';
    }else{
       $scope.img_name = name;
    }
};
<div ng-controller="SeuController">
  <img ng-src="{{img_name}}">
</div>

Assuming your carregarLogo function is inside a controller. Note that in html I'm using a native angle directive, ngSrc .

Now returning to the subject "use jQuery within angular". Whenever you need to do this, create a directive to put your DOM manipulations inside. And every time you change values from $ scope through jQuery functions use the $scope.apply() soon afterwards to update the scope values (within the directive).

    
04.02.2016 / 14:51