Aymn Alaney
Aymn Alaney

Reputation: 523

Why upload file did not work with ion-content? and work if I remove ion-content

I am new with ionic, and I want to upload picture, thus I use "ng-file-upload" to upload my picture. but the code not work only if I remove "ion-content". my template is:

    <ion-view view-title="IQ Marketplace" ng-controller="SellCtrl">
    <ion-content class="padding" >
    <div class="list">
      <form   ng-submit="submitAds()" name="form" novalidate>


        <label class="item item-input">
          <span class="input-label">Picture</span>
      <input type="file" ngf-select ng-model="file" name="file" ngf-pattern="'image/*'"
        accept="image/*" >
        </label>
    <input type="submit" class="button button-block button-positive" value="Save" ng-disabled="form.$invalid">
    </form>

    </div><!--list-->

    </ion-content>
    </ion-view>

And my controller is:

.controller('SellCtrl', function($scope,Upload) {

                  $scope.submitAds = function() {
                    console.log("ayman "+ $scope.file)
                      $scope.upload($scope.file);
                  };
                   $scope.upload = function (file) {
                      Upload.upload({
                          url: 'http://localhost/IQM/public/ads',
                          data: {file: file, 'username': $scope.test}
                      }).then(function (resp) {
                          console.log('Success ' + resp.config.data.file.name + 'uploaded. Response: ' + resp.data);
                      }, function (resp) {
                          console.log('Error status: ' + resp.status);
                      }, function (evt) {
                          var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
                          console.log('progress: ' + progressPercentage + '% ' + evt.config.data.file.name);
                      });
                  };
  });

This code gives me this exception:

   TypeError: Cannot read property 'name' of undefined
   at controllers.js:84
   at ionic.bundle.js:23476
   at Scope.$eval (ionic.bundle.js:24673)
   at Scope.$digest (ionic.bundle.js:24484)
   at ionic.bundle.js:24712
   at completeOutstandingRequest (ionic.bundle.js:14221)
   at ionic.bundle.js:14493

But when I remove the "ion-content" the upload will successful. What is the problem?

Upvotes: 5

Views: 473

Answers (2)

Aymn Alaney
Aymn Alaney

Reputation: 523

I solve the problem by adding $parent,thus it become ng-model="$parent.file"

<ion-view view-title="IQ Marketplace" ng-controller="SellCtrl">
<ion-content class="padding" >
<div class="list">
  <form   ng-submit="submitAds()" name="form" novalidate>


    <label class="item item-input">
      <span class="input-label">Picture</span>
  <input type="file" ngf-select ng-model="$parent.file" name="file" ngf-pattern="'image/*'"
    accept="image/*" >
    </label>
<input type="submit" class="button button-block button-positive" value="Save" ng-disabled="form.$invalid">
</form>
</div><!--list-->
</ion-content>
</ion-view>

Upvotes: 0

Laohyx
Laohyx

Reputation: 303

I guess you have introduced the Ionic Framework.

This apparently is angular's scope problem.

You define a ng-model="file" inside ion-content. While ion-content creates a new scope, so you cannot retrieve the variable file directly.

Please refer to the doc, and it says that:

Be aware that this directive gets its own child scope.

You can use dot notations like:

// controller
$scope.data = {}

// html
<ion-content class="padding" >
    ...
    <input ng-model="data.file" />
    ...
</ion-content>

And then you can get the value by $scope.data.file.

The root reason is about AngularJS's scope conception.

You can read these articles for more information.

Upvotes: 4

Related Questions