简体   繁体   中英

AngularJS upload photo without form submit

I want to upload a photo without hitting the having a submit button. I am using ionic framework and cordova plugin and the example below is getting a photo from the iphone's photo library.. for brevity i only included what's necessary for my request.

my view looks like this:

<form ng-submit="???">
      <img class="img-circle" ng-src="{{prof_pic}}" ngclick="showActionsheet()" ng-if="pic_is_not_null()"/>
</form>

controller:

$scope.getPictureFromGallery = function() {  $cordovaCamera.getPicture(secondoptions).then(function(imageData) {

     $scope.imageData = imageData;
     //data model
      $scope.data = {prof_pic_link: imageData};
      var image = angular.toJson($scope.data);

     UserService.UpdateMyPhoto($localStorage.CurrentUser.id, $localStorage.CurrentUser.auth_token, image)
  .success(function (data) {

       $scope.prof_pic = "data:image/jpeg;base64," + imageData;
       //to display the image in the view

          }).
        error(function(error,status) {
          console.log(error);
          console.log(status);

      })   


    }, function(err) {
      // error
    });

  };

You already have the answer in your question. You can bind ng-click to a function that uploads the image to your server.

But more of a question is why you would not want the submit-button. Ponder this: The user whishes to upload an image, she selects an image that is to be uploaded but manages to select the wrong one. If I read your scenario correctly, this will mean that the wrong image gets uploaded. If, on the other hand, there would be a submit-button the user can select a new image without the first one being uploaded.

So while you can do this, and you already have the answer yourself ( ng-click="myUploadFunction(image)" ), do you really want to?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM