简体   繁体   中英

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

I'm looking at the source code for the angular plugin of JQuery File upload, and I see the following code:

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

so it seems obvious to me that the module was written to allow for me to update the options on the fileupload widget (which is what $element.fileupload('option', ...) does); however, I'm not sure how to get to the $attrs.fileUpload.

I need to update the options to fileupload after an async call in my controller:

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

My current solution is a hack, and it is to mess with the options on a event callback (as described in How to change dynamically the upload (jquery-file-upload) url using $scope? ). I consider this a hack because it requires user interaction for the options to get set, and also leads to a race condition where user interaction might occur before the async call is complete.

Do you have the view code?

I would imagine you would need to update the options attribute on the controller.

eg

View:

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

Controller:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM