-1

In angularjs if i change or remove image it does not make any changes. After refreshing the page it show the changed image.I am using following line to remove image

jQuery

$('#profile_image').val('')
Maher
  • 2,517
  • 1
  • 19
  • 32
vivek
  • 383
  • 2
  • 23
  • so why you using angular when you already want to do this by `jquery`? – Maher Jun 13 '17 at 04:44
  • ok fine but how can i do it from angular? – vivek Jun 13 '17 at 04:54
  • Hi, What is the format of the image you are displaying? Url or Base64?? – Alexis Jun 13 '17 at 05:06
  • Hi,Actually i am inserting image path in database and store image in folder.from that folder i am showing that corresponding image by mysql query.I really donot konow what is URL or basse64 image format – vivek Jun 13 '17 at 05:10

2 Answers2

0

This Sample is basic if you want to know more about AngularJs click on link

var app = angular.module("app", []);

app.controller("ctrl", [
  "$scope",
  function($scope) {
    $scope.imgShow = true;
    $scope.imgSrc = "https://www.gravatar.com/avatar/75025ad9a8cfdaa5772545e6e8f41133?s=32&d=identicon&r=PG&f=1";

    $scope.display = function() {
      $scope.imgShow = true;
      $scope.imgSrc = "https://www.gravatar.com/avatar/75025ad9a8cfdaa5772545e6e8f41133?s=32&d=identicon&r=PG&f=1";
    }

    $scope.change = function() {
      $scope.imgSrc = "https://www.gravatar.com/avatar/fccd71b79b3571b459cdfe40e7bf5dd8?s=32&d=identicon&r=PG&f=1";
    }

    $scope.remove = function() {
      $scope.imgShow = false;
    }

  }
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
  <img ng-src="{{imgSrc}}" ng-show="imgShow">
  <hr/>
  <button ng-click="display()" ng-hide="imgShow">display image</button>
  <button ng-click="change()">change image</button>
  <button ng-click="remove()">remove image</button>
</div>
Maher
  • 2,517
  • 1
  • 19
  • 32
  • Actually i am using to add user defined image.when i want to remove image i am using Change when i want to add image – vivek Jun 13 '17 at 05:25
  • @Vivekvetrri Do not use angular.element(this).scope() since it will not work in case you'll disable debug info for production, as recommended in the docs. .scope() method should be used only for testing/debugging. – Stanislav Kvitash Jun 13 '17 at 09:28
  • @Vivekvetrri Please update the question about `` usage, since the question is not formed well. – Stanislav Kvitash Jun 13 '17 at 09:51
0

If you want to manipulate the DOM in AngularJS you should use directives for this purpose.

You should avoid using jquery inside of your controllers and simply work with your model. Below is fileUpload directive that could help you to work with <input type="file"> in a more angularjs-way:

angular.module('myApp', [])
.controller('TestController', ['$scope', function ($scope) {
    var ctrl = this;
    
    ctrl.imageFile = null;
    ctrl.clearFile = clearFile;
    
    function clearFile(){
      ctrl.imageFile = null;
    }
    
    $scope.$ctrl = ctrl;
}])
.directive('fileUpload', [function () {
  return {
    require: "ngModel",
    restrict: 'A',
    link: function ($scope, el, attrs, ngModel) {
      function onChange (event) {
        //update bindings with $applyAsync
        $scope.$applyAsync(function(){
          ngModel.$setViewValue(event.target.files[0]);
        });
      }
      //change event handler
      el.on('change', onChange);
      
      //set up a $watch for the ngModel.$viewValue
      $scope.$watch(function () {
        return ngModel.$viewValue;
      }, function (value) {
        //clear input value if model was cleared
        if (!value) {
          el.val("");
        }
      });
      //remove change event handler on $destroy
      $scope.$on('$destroy', function(){
        el.off('change', onChange);
      });
    }
  };
}]);
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//code.angularjs.org/1.3.20/angular.js"></script>

<div ng-app="myApp">
  <div ng-controller="TestController">
    <input type="file" file-upload ng-model="$ctrl.imageFile" />
    <input type="button" ng-click="$ctrl.clearFile()" value="Reset" />
    <div ng-if="$ctrl.imageFile">
      {{$ctrl.imageFile.name}}<br />
      {{$ctrl.imageFile.size}} byte(s)<br/>
      {{$ctrl.imageFile.type}}
    </div>
  </div>
</div>

UPDATE: Also take a look at this useful reading.

Stanislav Kvitash
  • 4,614
  • 18
  • 29