0

I have a ng-repeat directive to show 6 pictures. I do an AJAX call to get the photos from the server to the app. All the data is stored in a scope var called vendor data. Then, in HTML code I do an ng-repeat to show the pictures. There is a button below each picture to take another from the gallery and change it. Here is the code:

HTML code

   <div ng-repeat="photo in vendordata.galerias" class="elemento">
        <div style="z-index: 10;">
          <img class="thumb" ng-src="{{photo.url}}" />
        </div>
        <div class="" style="text-align: center;">
          <button class="" href="#" ng-click="changePic($index)">Change picture</button>
        </div>
      </div>
    </div>

Controller Code:

$scope.changePic = function(identifier){
    $scope.elementSek = identifier;
    $scope.getPhoto(pictureSource.PHOTOLIBRARY);
};
$scope.getPhoto = function (source) {
  // Retrieve image file location from specified source
  navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,
    destinationType: destinationType.FILE_URI,
    sourceType: source });
};
function onPhotoURISuccess(imageURI) {
  // Get image handle
  $scope.vendordata.galerias[$scope.elementoSek].url = imageURI;
}
// Called if something bad happens.
//
function onFail(message) {
  console.log('Failed because: ' + message);
}

The var has the correct value, before and after the change. But the img don't updates the selected picture. How can I update it?

5
  • Which path returns photo.url ? Commented Jan 26, 2016 at 12:15
  • Have you tried wrapping the $scope.vendordata.galerias[$scope.elementoSek].url = imageURI; into a $scope.$apply ? Commented Jan 26, 2016 at 12:20
  • Before selecting new picture it returns http://domain.com/galleries/picture.jpg, after the change it returns file://path_to_new_file.jpg Commented Jan 26, 2016 at 12:22
  • Great idea, I will apply scope now to test. Commented Jan 26, 2016 at 12:23
  • It works @nubinub, please, add an answer in order to mark it as correct and give +1 to you. And thanks a lot! Commented Jan 26, 2016 at 17:00

1 Answer 1

2
function onPhotoURISuccess(imageURI) {
  // Get image handle
  $scope.$apply(function(){
    $scope.vendordata.galerias[$scope.elementoSek].url = imageURI;
  });
}

Since the navigator.camera.getPicturefunction you are using is not part of angular, you need to wrap the callback into a $scope.$apply in order to trigger a $digestcycle which will make your model changes effective on the view.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for your help @nubinub it works perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.