为什么$ scope.logo在控制器中未定义?

时间:2017-05-25 12:26:49

标签: html angularjs node.js

这是我的HTML表单

<form ng-submit='create()'>
    ..
    .
    .
    <input type='file' ng-model='logo' accept="image/*">
</form>

这是我的控制者:

$scope.create = function () {
        $scope.Ent = {}
        $scope.Ent.logo = $scope.logo;

This is my console only input file is undefined and all other fileds are alright

1 个答案:

答案 0 :(得分:1)

ng-model无法使用输入类型&#39;文件。使用自定义指令绑定它

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

将范围变量分配给fileread属性

<form ng-submit='create()'>
    ..
    .
    .
    <input type='file' fileread='logo' accept="image/*">
</form>