简体   繁体   English

AngularJS上传照片而不提交表单

[英]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. 我正在使用ionic框架和cordova插件,下面的示例是从iphone的照片库中获取照片。为简便起见,我只包含了我的要求所必需的内容。

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. 您可以将ng-click绑定到将图像上传到服务器的功能。

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? 因此,尽管您可以做到这一点,并且已经有了答案( ng-click="myUploadFunction(image)" ),但您真的要这么做吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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