单击按钮时将ng-model分配给输入类型文件

时间:2017-04-03 04:14:05

标签: javascript html angularjs

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <script>
    var app = angular.module("myApp",[]);
    app.controller("myCtrl", function($scope) {
    $scope.users=[ 
     {name:'',email:'',course:'',file:''}
      ]
      $scope.value=function(){
       $scope.users=[{             
         name:'abc',
         email:'abc@gmail.com',
         course:'angular js,css',
         file:'abc.jpg';
       }];
     }
     });
   </script>
         <div ng-app="myApp" ng-controller="myCtrl">
     <button type="button" ng-click="value()">To get values</button>
   <div ng-repeat="user in users">
     <input type="text" ng-model="user.name">
     <input typee="text" ng-model="user.email">
     <select ng-model="user.course" multiple>
       <option value="html">html </option>
       <option value="css">css</option>
       <option value="angular js">angular js</option>
     </select>
     <input type="file" ng-model="user.file">
    </div>
  </div>

我是角色js的新手我想要将文件名分配给输入类型文件时单击按钮获取值我想只分配值并显示输入类型文件的值以及如何绑定ng-model对于多选框

1 个答案:

答案 0 :(得分:0)

.directive("fileread", [function () {
return {
    scope: {
        fileread: "="
    },
    link: function (scope, element, attributes) {
        element.bind("change", function (changeEvent) {
            var reader = new FileReader();
            reader.onload = function (loadEvent) {
                scope.$apply(function () {
                    scope.fileread = loadEvent.target.result;
                });
            }
            reader.readAsDataURL(changeEvent.target.files[0]);
        });
    }
}

}]);

<input type="file" fileread="vm.uploadme" />