简体   繁体   English

AngularJS&D3:D3的Angular指令多次加载

[英]AngularJS & D3 : Angular directive for D3 load multiple times

I wrote an angular directive for d3 forced-directed graph. 我为d3强制定向图编写了一个角度指令。 Code is here. 代码在这里。 I use $log.log("xx"); 我使用$ log.log(“ xx”); to debug the code. 调试代码。 I realized for some reasone this directive load multiple times. 我出于某种原因意识到该指令多次加载。 For the mg-controller, the one the directive bond with, I have 2 factories and these two factories are called whenever the controller is initialized. 对于mg-controller,一个与之绑定的指令,我有2个工厂,并且每当初始化控制器时就会调用这两个工厂。 By using $log.log("xx"); 通过使用$log.log("xx"); I realized when the pay is loaded, the d3 directive at least load 6 times, twice for each factory and twice after. 我意识到在加载薪水时,d3指令至少加载6次,每个工厂两次,之后两次。 The way I "got around" is using d3.select("svg") .remove(); 我“绕开”的方式是使用d3.select("svg") .remove(); so my page won't have duplicates svgs. 这样我的页面就不会有重复的svg。 But this highly affects the performance. 但这会严重影响性能。 Beside, I added some costumed filter in the same page under the same controller, and I noticed whenever the filter runs, the d3 directive will reload. 另外,我在同一控制器下的同一页面中添加了一些带有修饰符的过滤器,并且我注意到只要过滤器运行,d3指令就会重新加载。 The filter has not thing to do with the d3 directive, except they are under the same controller.(I don't think the controller reload when the filter runs.) 过滤器与d3指令无关,除了它们在同一控制器下(我不认为过滤器运行时控制器会重新加载)。

I believe the issue is in my link: function part. 我相信问题出在我的link: function部分。 It seems messy but it actually only does two things: 1) generates data the will be feed to d3 2)do some d3 and generates the graph. 看起来很杂乱,但实际上它只做两件事:1)生成将被馈送到d3的数据2)做一些d3并生成图形。

template:'<div>{{fdg()}}<div>',
            link: function ( $scope, $element,attr){
                             ...
                        $scope.fdg = function(){
                            $log.log("in function");
                            $scope.getLinks();
                            ForceDirectedGraph($scope.tags,$scope.links);
                        }

I put two functions into $scope.fdg and invoke it in template:'<div>{{fdg()}}<div>' . 我将两个函数放入$scope.fdg并在template:'<div>{{fdg()}}<div>'调用它template:'<div>{{fdg()}}<div>' This is probably a very bad practice, but I couldn't figure out other ways to invoke the function. 这可能是一个非常糟糕的做法,但我无法找出其他方法来调用该函数。

I tried to invoke these two functions 我试图调用这两个功能

 $scope.getLinks();
 ForceDirectedGraph($scope.tags,$scope.links);

Directly in the link: function like this: 直接在link: function是这样的:

link: function ( $scope, $element,attr){
                            $log.log($scope.tags);
                            $log.log(tags);
                            $scope.getLinks();
                            ForceDirectedGraph($scope.tags,$scope.links);

Strangely, $scope.tags is empyty, "[]" and 'tags' is undefined. 奇怪的是, $scope.tags是空的,“ []”和'tags'是未定义的。 I passed 'tags' in view by <forcedirected-graph tags="tags" style="overflow: hidden;"></forcedirected-graph> I truly could not understand why the function could not get the argument. 我通过<forcedirected-graph tags="tags" style="overflow: hidden;"></forcedirected-graph>在视图中传递了“标签”,我真的不明白为什么函数无法获取参数。 That's why I stupidly use $scope.fdg.function()... to wrap these two functions so they could reach to the argument passed from view. 这就是为什么我愚蠢地使用$scope.fdg.function()...包装这两个函数,以便它们可以到达从视图传递的参数。

I have some other (d3) directives that loads normally. 我还有其他一些正常加载的(d3)指令。 I try to make the template of the abnormal d3 directive to match those working directives, but I failed :( (I am confused by how arguments/scope actually works in directive link). 我试图使异常d3指令的模板与那些工作指令匹配,但是我失败了:((我对参数/作用域在指令链接中的实际工作方式感到困惑)。

More completed codes: here and here . 更完整的代码: 在这里这里

It will be great if I could figure out what's wrong so I could gain a deeper understanding in Angular. 如果我能找出问题所在,那将会很棒,这样我就可以对Angular有了更深入的了解。 Angular is really awesome :) Angular真的很棒:)

Thanks in advance! 提前致谢!

Are you using an ng-repeat somewhere? 您在某处使用ng-repeat吗?
I remember realising that the link-function get´s executed multiple times for each element in the ng-repeat. 我记得我意识到对ng-repeat中的每个元素多次执行链接功能。 So maybe in this case it´s better to use the compile function instead. 因此,在这种情况下,也许最好使用compile函数。 Check this article: http://www.jvandemo.com/the-nitty-gritty-of-compile-and-link-functions-inside-angularjs-directives/ 检查本文: http : //www.jvandemo.com/the-nitty-gritty-of-compile-and-link-functions-inside-angularjs-directives/

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

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