为什么上传文件不适用于离子内容?如果我去除离子含量,我的工作

Aym*_*ney 5 angularjs ionic

我是离子的新手,我想上传图片,因此我使用"ng-file-upload"上传我的图片.但是只有当我删除"ion-content"时代码才能工作.我的模板是:

    <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>
Run Code Online (Sandbox Code Playgroud)

我的控制器是:

.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);
                      });
                  };
  });
Run Code Online (Sandbox Code Playgroud)

这段代码给了我这个例外:

   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
Run Code Online (Sandbox Code Playgroud)

但是,当我删除"离子内容"时,上传将成功.问题是什么?

小智 4

我猜你已经介绍了Ionic Framework

这显然是角度的scope问题。

您定义一个ng-model="file"内部ion-content. 虽然ion-content创建了一个新的scope,所以您不能直接检索该变量file

请参阅文档,它说:

请注意,该指令有其自己的子作用域。

你可以dot notations这样使用:

// controller
$scope.data = {}

// html
<ion-content class="padding" >
    ...
    <input ng-model="data.file" />
    ...
</ion-content>
Run Code Online (Sandbox Code Playgroud)

然后就可以通过 获取值了$scope.data.file

根本原因在于 AngularJS 的scope理念。

您可以阅读这些文章以获取更多信息。