【问题标题】:AngularJS: Access factory from within decoratorAngularJS:从装饰器内部访问工厂
【发布时间】:2014-12-12 00:20:37
【问题描述】:

我正在使用装饰器扩展第三方指令。我想访问我在装饰器中的一个工厂。我该怎么做?

$provide.decorator( 'multiSelectDirective', function( $delegate ) {
    var directive = $delegate[0],
        link = directive.link;

    // wipe out the shitty template
    directive.template = '';

    // make with the new template!
    directive.templateUrl = 'app/partials/filters.template.html';

    // hook into the compile phase of the directive
    directive.compile = function( ) {

        // the function returned by compile is the new link function
        return function( $scope, el, attrs ) {

            // run the original link function.
            link.apply(this, arguments);

            $scope.filterClicked = function( buttonName, selection ) {
                handleFilterClick( buttonName, selection, JiraData, GreyGooseApi );             
            }

        }
    };

    return $delegate;
});

【问题讨论】:

    标签: angularjs decorator


    【解决方案1】:

    我想通了。我能够通过以下方式注入依赖项:

    $provide.decorator( 'multiSelectDirective',
        [ '$delegate', 'JiraData', 'GreyGooseAPI',
        function( $delegate, JiraData, GreyGooseApi ) {
            var directive = $delegate[0],
                link = directive.link;
    
            // wipe out the shitty template
            directive.template = '';
    
            // make with the new template!
            directive.templateUrl = 'app/partials/filters.template.html';
    
            // hook into the compile phase of the directive
            directive.compile = function( ) {
    
                // the function returned by compile is the new link function
                return function( $scope, el, attrs ) {
    
                    // run the original link function.
            link.apply(this, arguments);
    
            $scope.filterClicked = function( buttonName, selection ) {
                handleFilterClick( buttonName, selection, JiraData, GreyGooseApi );
            }
    
                }
            };
    
            return $delegate;
        }]);
    
    });
    

    【讨论】:

      【解决方案2】:

      您可以通过$delegate.someproperty 访问工厂公开的所有属性,如果未公开,则无法访问该属性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-25
        • 2011-11-27
        • 1970-01-01
        相关资源
        最近更新 更多