简体   繁体   English

Cytoscape.js指令不会在调整容器大小时使尺寸无效

[英]Cytoscape.js directive not invalidating dimensions when container is resized

I have a cytoscape.js graph inside an angular.js directive listening (watching) for it's container div width. 我在angular.js指令中有一个cytoscape.js图,用于侦听(监视)容器的div宽度。 when I'm collapsing a side panel in my app, the container div takes the whole width of the screen, then a $watch should run, and do a cy.resize() , however it's not done as I hoped it would. 当我在应用程序中折叠侧面板时,容器div占据了屏幕的整个宽度,然后应该运行$watch并执行cy.resize() ,但是并没有实现我希望的那样。

in my link function i'm calling the container 在我的链接函数中,我正在调用容器

var container = angular.element(document.querySelector('#cy'));

it works, i'm getting the container, and setting up the graph and it's showing fine. 它有效,我正在获取容器,并设置了图形,并且显示良好。

then later, when container div width changes 然后,当容器div宽度改变时

scope.$watch(function () {
        return document.querySelectorAll("#cy")[0].clientWidth;
    }, function (newVal, oldVal) {
        if (newVal !== oldVal) {
            if (typeof cy.resize === 'function') {
                cy.resize();
            }
        }
    });

the $watch runs as it should, the div does get wider but the container just moves aside to the newly clear area. $ watch可以正常运行,div的确会变宽,但是容器只是移到新的空白区域。 the map isn't invalidated on .resize() , what I get is that the mouse position is calculated wrong. 该地图没有在.resize()上失效,我得到的是鼠标位置计算错误。 when I re-size the browser window it does resize properly (as the docs mention). 当我调整浏览器窗口的大小时,它确实会正确调整大小(如文档所述)。

how can I force a resize in my case ? 我该如何调整大小?

It's very likely that your listener set up through Angular isn't working as you expect. 通过Angular设置的侦听器很可能无法按预期工作。 Angular is not a reliable way to set up listeners, unless everything you're listening to lives in Angular's model and updates in sync with Angular's update cycle. 除非您要监听的所有内容都位于Angular的模型中并且与Angular的更新周期同步进行更新,否则Angular不是设置侦听器的可靠方法。 That's a lot of restrictions, and I don't think Angular will work for what you want here. 这有很多限制,我认为Angular不能满足您在这里的需求。

Bind to proper events instead, or call cy.resize() in each of your functions that resizes the div. 而是绑定到适当的事件,或在调整div大小的每个函数中调用cy.resize()

After a long try and miss streak, we ended up doing this process 经过长时间的尝试和错过连胜,我们最终完成了此过程

  1. in the controller we hooked up to the window.onresize() event. 在控制器中,我们连接到window.onresize()事件。 because we used controllerAs we had to define a scope an object to watch a boolean that went true when the window got sized to a larger width, using a getter and a setter, like so 因为我们使用controllerAs我们必须定义一个范围,一个对象来观察布尔值,当使用getter和setter将窗口调整为更大的宽度时,布尔值变为真,就像这样

     Object.defineProperty(vm, "toggleNav", { get: function () { return navToggled; }, set: function (newValue) { navToggled = newValue; if (angular.isFunction(vm.resizeControl.onresize)) { vm.resizeControl.onresize(); } }}); vm.resizeControl = {}; 
  2. in the directive we watched for the scope property ( resizeControl ) change 在指令中,我们观察了范围属性( resizeControl )的变化

     scope.resizeControl.onresize = function () { if (angular.isObject(cy) && angular.isFunction(cy.resize)) { $timeout(function () { cy.resize(); }); } }; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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