ng-model for `<input type=“file”/>` (with directive DEMO)

后端 未结 12 2276
北恋
北恋 2020-11-21 06:40

I tried to use ng-model on input tag with type file:


But after selecting a file, in

12条回答
  •  醉酒成梦
    2020-11-21 06:56

    How to enable to work with ng-model

    Working Demo of Directive that Works with ng-model

    The core ng-model directive does not work with out of the box.

    This custom directive enables ng-model and has the added benefit of enabling the ng-change, ng-required, and ng-form directives to work with .

    angular.module("app",[]);
    
    angular.module("app").directive("selectNgFiles", function() {
      return {
        require: "ngModel",
        link: function postLink(scope,elem,attrs,ngModel) {
          elem.on("change", function(e) {
            var files = elem[0].files;
            ngModel.$setViewValue(files);
          })
        }
      }
    });
    
      
        

    AngularJS Input `type=file` Demo

    NameDateSizeType
    {{file.name}} {{file.lastModified | date : 'MMMdd,yyyy'}} {{file.size}} {{file.type}}

提交回复
热议问题