javascript - 错误 : [$compile:multidir] for Component Directive with Attribute Directive

标签 javascript angularjs angularjs-directive angularjs-components

我需要一个“粘性”指令,当它位于页面顶部时向元素添加一个 css 类,并且还指示其状态的变化。出于这个原因,我定义了一个范围,如 { onStickyChange: '&' }。现在我想在 angularjs 组件中使用该指令,例如:

<my-component sticky on-sticky-change="$ctrl.onStickyChange(sticky)">
</my-component>

我希望指令在我的组件被粘贴/取消粘贴时通知父 Controller 。但是我收到以下错误:

Error: [$compile:multidir] Multiple directives [myComponent, sticky] asking for new/isolated scope on: http://errors.angularjs.org/1.6.2/$compile/multidir?p0=myComponent&p1=&p2=s…icky%3D%22%22%20on-sticky-change%3D%22%24ctrl.onStickyChange(sticky)%22%3E at angular.js:68 at assertNoDuplicate (angular.js:10049) at applyDirectivesToNode (angular.js:9237) at compileNodes (angular.js:8826) at compileNodes (angular.js:8838) at compileNodes (angular.js:8838) at compile (angular.js:8707) at angular.js:1847 at Scope.$eval (angular.js:18017) at Scope.$apply (angular.js:18117)

app.component('myComponent', {
    template: '<div style="height: 6000px; width: 100%; background-color: #ccf></div>',
    controller: function () {
        this.is = 'nothing';
    }
});
app.directive('sticky', ['$window', function($window) {
    return {
        restrict: 'A',
        scope: { onStickyChange: '&' },
        link: link
    };
    function link(scope, element, attributes) {
        if (typeof scope.onStickyChange !== 'function' ) {
            throw Error('Sticky requires change handler');
        }

        let sticky = isSticky(element);

        angular.element($window).bind('scroll', _.throttle(onWindowScroll, 60));

        function onWindowScroll() {
            let isNowSticky = isSticky(element);

            if (sticky === isNowSticky) {
                return;
            }

            sticky = isNowSticky;

            if (sticky) {
                element.addClass('sticky');
            }
            else {
                element.removeClass('sticky');
            }

            scope.onStickyChange({ sticky: sticky });
        }

        function isSticky(element) {
            return window.scrollTop() > element.position().top;
        }
    }

}]);

如何解决这个问题?

PS:有一个plunk .

最佳答案

错误发生是因为组件指令和属性指令都试图创建一个隔离作用域。

来自文档:

Error: $compile:multidir

Multiple Directive Resource Contention

This error occurs when multiple directives are applied to the same DOM element, and processing them would result in a collision or an unsupported configuration.

Example scenarios of multiple incompatible directives applied to the same element include:

  • Multiple directives requesting isolated scope.

— AngularJS Error Reference - Error: $compile:multidir

解决方案是重写属性指令以在不创建隔离范围的情况下工作:

app.directive('sticky', function($window, $parse) {
    return {
        restrict: 'A',
        ̶s̶c̶o̶p̶e̶:̶ ̶{̶ ̶o̶n̶S̶t̶i̶c̶k̶y̶C̶h̶a̶n̶g̶e̶:̶ ̶'̶&̶'̶ ̶}̶,̶
        scope: false,
        link: postLink
    };
    function postLink(scope, elem, attrs) {

        //code ...

            ̶s̶c̶o̶p̶e̶.̶o̶n̶S̶t̶i̶c̶k̶y̶C̶h̶a̶n̶g̶e̶(̶{̶ ̶s̶t̶i̶c̶k̶y̶:̶ ̶s̶t̶i̶c̶k̶y̶ ̶}̶)̶;̶
            $parse(attrs.onStickyChange)(scope, { sticky: sticky });

        //code ...
    }
});

使用 $parse Serviceon-sticky-change 属性上评估 Angular Expression。

关于javascript - 错误 : [$compile:multidir] for Component Directive with Attribute Directive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45196150/

相关文章:

javascript - html5文件上传

javascript - 使用选择器作为变量循环 .append()

javascript - 跨屏幕的 CSS 线性动画

javascript - angularjs在创建后将参数传递给模态

javascript - 当我使用 Angular Material 和一些自定义代码时如何解决 Angular 应用程序中的 $event 问题

javascript - 在指令中使用 Controller 属性/方法

javascript - 在 JAVASCRIPT 中运行 FFMPEG 命令

node.js - Angular 服务快速 API 路由 + 发布数据

angularjs - 如何在单击标记时重定向到下一页并显示有关标记的详细信息?

javascript - 数组长度内部函数不同