【问题标题】:Get form input filename using AngularJS使用 AngularJS 获取表单输入文件名
【发布时间】:2014-02-28 17:13:51
【问题描述】:

我试图在用户点击提交之前检查文件的名称。我可以像获取输入字段的内容一样获取文件名吗?

类似:

<input name="posterTitle" type="text" ng-model="posterTitle">
{{posterTitle}}

类似:

<input name="posterFileName" ng-model="posterFileName" type="file" />

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    使用 Angularjs,您可能需要使用 onchange 事件来绑定控制器内的输入名称。看例子:

    <input name="posterFileName" type="file" onchange="angular.element(this).scope().fileName(this)"/>
    

    内部控制器

    $scope.fileName= function(element) {
       $scope.$apply(function($scope) {
          $scope.posterTitle= element.files[0].name;
       });
    };
    

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      我发现的几个链接可能会有所帮助: http://api.jquery.com/file-selector/ jQuery: get the file name selected from <input type="file" />

      例如,唱名或ID属性:

      $('input[type=file]').change(function(e){ $in=$(这个); $in.next().html($in.val()); });

      【讨论】:

      • 我实际上是在寻找一种角度方法。我需要与我的角度控制器中的文件名进行比较。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-08
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 2017-07-10
      相关资源
      最近更新 更多