加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 为什么上传文件不能用于离子内容?如果我去除离子

发布时间:2020-12-17 06:48:03 所属栏目:安全 来源:网络整理
导读:我是离子的新手,我想上传图片,因此我使用“ng-file-upload”上传我的图片. 但是只有当我删除“ion-content”时代码才能工作.我的模板是: ion-view view-title="IQ Marketplace" ng-controller="SellCtrl" ion-content class="padding" div class="list" for
我是离子的新手,我想上传图片,因此我使用“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>

我的控制器是:

.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);
                      });
                  };
  });

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

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

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

解决方法

我想你已经介绍了 Ionic Framework.

这显然是角度范围问题.

您可以在离子内容中定义ng-model =“file”.虽然ion-content创建了一个新范围,但您无法直接检索变量文件.

请参考doc,它说:

Be aware that this directive gets its own child scope.

您可以使用点符号,例如:

// controller
$scope.data = {}

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

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

根本原因在于AngularJS的范围概念.

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

> http://forum.ionicframework.com/t/problem-with-ion-content-and-scope-vars-inside-function/1811
> https://github.com/angular/angular.js/wiki/Understanding-Scopes

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读