0

我正在查看 JQuery File upload 的 angular 插件的源代码,我看到以下代码:

angular.module('blueimp.fileupload', [])
  // The fileUpload service provides configuration options
  // for the fileUpload directive and default handlers for
  // File Upload events:
  .provider('fileUpload', function () { ... })
  .controller('FileUploadController', [
        '$scope', '$element', '$attrs', '$window', 'fileUpload',
        function ($scope, $element, $attrs, $window, fileUpload) {

   ...
   // Observe option changes:
   $scope.$watch(
       $attrs.fileUpload,
       function (newOptions) {
           if (newOptions) {
               $element.fileupload('option', newOptions);
           }
       }
   );

所以对我来说很明显,该模块的编写允许我更新文件上传小部件上的选项(这就是 $element.fileupload('option', ...) 所做的);但是,我不确定如何访问 $attrs.fileUpload。

在我的控制器中进行异步调用后,我需要更新文件上传的选项:

var accountEditor = angular.module('accountEditor', [ 'ngResource', 'blueimp.fileupload' ]);
accountEditor.controller('accountEditorCtrl', [
            '$scope',
            '$resource',
            'fileUpload',
            function($scope, $resource, fileUpload) {
             doAsyncThing(function() {
                 // update options...what goes here?
             });

我当前的解决方案是一个 hack,它是弄乱事件回调的选项(如如何使用 $scope? 动态更改上传(jquery-file-upload)url 中所述)。我认为这是一种 hack,因为它需要用户交互才能设置选项,并且还会导致在异步调用完成之前可能发生用户交互的竞争条件。

4

1 回答 1

0

有查看代码吗?

我想你需要更新控制器上的选项属性。

例如

看法:

<div data-ng-controller="accountEditorCtrl">
    <form data-ng-controller="FileUploadController" data-file-upload="options">
    </options>
</div>

控制器:

var accountEditor = angular.module('accountEditor', [ 'ngResource', 'blueimp.fileupload' ]);
accountEditor.controller('accountEditorCtrl', [
        '$scope',
        '$resource',
        'fileUpload',
        function($scope, $resource, fileUpload) {

        $scope.options = {}; // Set options here

         doAsyncThing(function() {
             $scope.options = {}; // Update options
             // Note if this async method is not handled by angular you may need
             // to call $scope.$apply(); to notify angular of the changes to scope
         });
于 2013-11-13T23:46:23.417 回答