簡體   English   中英

如何修改angular中注入的依賴項的依賴項?

[英]How do I modify a dependency of an injected dependency in angular?

我正在查看JQuery File上傳的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);
           }
       }
   );

因此,對我來說顯而易見的是,該模塊被編寫為允許我更新fileupload小部件上的選項(這是$ 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?
             });

我當前的解決方案是破解,它會弄亂事件回調中的選項(如如何使用$ scope?動態更改上載(jquery-file-upload)URL中所述 )。 我認為這是一種破解,因為它需要用戶交互才能設置選項,並且還會導致競爭狀態,在異步調用完成之前,可能會發生用戶交互。

您有查看代碼嗎?

我想您將需要更新控制器上的options屬性。

例如

視圖:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM