0

我有以下代码。

HTML:

<div class="button" ngf-select ng-model="files">Upload using model $watch</div>
<img width="80" height="80" ngf-src="files[0]" ngf-default-src="'/thumb.jpg'" ngf-accept="'image/*'">

JAVASCRIPT:

$scope.$watch('[files]', function () {
        $scope.upload($scope.files);
        console.log($scope.files);
    });

    $scope.log = '';

    $scope.upload = function (files) {
        if (files && files.length) {
            for (var i = 0; i < files.length; i++) {
                var file = files[i];
                Upload.upload({
                    url: 'https://angular-file-upload-cors-srv.appspot.com/upload',
                    fields: {
                        'username': $scope.username
                    },
                    file: file
                }).progress(function (evt) {
                    var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
                    $scope.log = 'progress: ' + progressPercentage + '% ' +
                                evt.config.file.name + '\n' + $scope.log;
                }).success(function (data, status, headers, config) {
                    $timeout(function() {
                        $scope.log = 'file: ' + config.file.name + ', Response: ' + JSON.stringify(data) + '\n' + $scope.log;
                    });
                });
            }
        }
    };

上面的代码在jsFiddle中运行良好,我也仔细遵循了这个文档。当您在出现在img width="80" height="80" ngf-src="files[0]" ngf-default-src="'/thumb.jpg'" ngf-accept="'image/*'">. 到目前为止,一切都很好。

$scope.$watch似乎只适用于 onload 和console.log($scope.files);prints undefined,第二次角度应该调用它不调用的摘要循环,$scope.$watch阻止它调用$scope.upload($scope.files);将执行上传的调用。什么都没有发生,好像ng-model="files"不起作用。我花了几个小时调试这个请帮忙。

4

0 回答 0