加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 多个指令[myPopup,myDraggable]要求新的/隔离的范

发布时间:2020-12-17 08:49:52 所属栏目:安全 来源:网络整理
导读:我写了一个指令对话框(myPopup)和另一个拖动这个对话框(myDraggable),但我总是得到的错误: Multiple directives [myPopup,myDraggable] asking for new/isolated scope 这是一个Plunker:http://plnkr.co/edit/kMQ0hK5RnVw5xOBdDq5P?p=preview 我能做什么
我写了一个指令对话框(myPopup)和另一个拖动这个对话框(myDraggable),但我总是得到的错误:

Multiple directives [myPopup,myDraggable] asking for new/isolated scope

这是一个Plunker:http://plnkr.co/edit/kMQ0hK5RnVw5xOBdDq5P?p=preview

我能做什么?

JS代码:

var app = angular.module('myApp',[]);

function myController($scope) {
    $scope.isDraggable = true;
}


app.directive('myPopup',[
    function () {
        "use strict";

        return {
            restrict: 'E',replace: true,transclude: true,template: '<div my-draggable="draggable"class="dialog"><div class="title">{{title}}</div><div class="content" ng-transclude></div></div>',scope: {
                title: '@?dialogTitle',draggable: '@?isDraggable',width: '@?width',height: '@?height',},controller: function ($scope) {
                // Some code
            },link: function (scope,element,attr) {
                if (scope.width) {
                    element.css('width',scope.width);
                }

                if (scope.height) {
                    element.css('height',scope.height);
                }                    
            }
        };
    }
]);

app.directive('myDraggable',['$document',function ($document) {
    return {
        restrict: 'A',replace: false,scope: { enabled: '=myDraggable' },elm,attrs) {
            var startX,startY,initialMouseX,initialMouseY;

            if (scope.enabled === true) {
                elm.bind('mousedown',function ($event) {
                    startX = elm.prop('offsetLeft');
                    startY = elm.prop('offsetTop');
                    initialMouseX = $event.clientX;
                    initialMouseY = $event.clientY;
                    $document.bind('mousemove',mousemove);
                    $document.bind('mouseup',mouseup);
                    $event.preventDefault();
                });
            }

            function getMaxPos() {
                var computetStyle = getComputedStyle(elm[0],null);
                var tx,ty;
                var transformOrigin =
                    computetStyle.transformOrigin ||
                    computetStyle.webkitTransformOrigin ||
                    computetStyle.MozTransformOrigin ||
                    computetStyle.msTransformOrigin ||
                    computetStyle.OTransformOrigin;
                tx = Math.ceil(parseFloat(transformOrigin));
                ty = Math.ceil(parseFloat(transformOrigin.split(" ")[1]));
                return {
                    max: {
                        x: tx + window.innerWidth - elm.prop('offsetWidth'),y: ty + window.innerHeight - elm.prop('offsetHeight')
                    },min: {
                        x: tx,y: ty
                    }
                };
            }

            function mousemove($event) {
                var x = startX + $event.clientX - initialMouseX;
                var y = startY + $event.clientY - initialMouseY;
                var limit = getMaxPos();
                x = (x < limit.max.x) ? ((x > limit.min.x) ? x : limit.min.x) : limit.max.x;
                y = (y < limit.max.y) ? ((y > limit.min.y) ? y : limit.min.y) : limit.max.y;
                elm.css({
                    top: y + 'px',left: x + 'px'
                });
                $event.preventDefault();
            }

            function mouseup() {
                $document.unbind('mousemove',mousemove);
                $document.unbind('mouseup',mouseup);
            }
        }
    };
}]);
从 docs:

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

Multiple directives requesting isolated scope.

Multiple directives publishing a controller under the same name.

Multiple directives declared with the transclusion option.

Multiple directives attempting to define a template or templateURL.

尝试删除隔离范围myDraggable的指令:

app.directive('myDraggable',//remove this line

将scope.enabled替换为attrs.enabled:

if (attrs.enabled == "true") {

并修改您的模板以绑定enable属性:

<div my-draggable="draggable" enabled="{{draggable}}"

DEMO

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读