当“ng斗篷”处于活动状态时,如何获取父元素的clientHeight



我在指令的链接器中有以下内容。。。。

pre: function preLink($scope, e) {
    var element = d3.select(e[0]);
    var height = element.node().parentNode.clientHeight;
    element
      .style({
        "border": "7px solid black",
        "min-height": height+"px",
        "background-image" : "url('http://s3.amazonaws.com/dostuff-production/property_assets/23107/yellow-stripes.png')"
      });
      element.select(".board")
                    .style("transform", function(d){
                        var ch = d3.select(this).node().clientHeight;
                        return "translate("+0+"px, "+(height/2-ch/2)+"px)"
                    })
}

然后在我的代码中我有

<jg-body ng-cloak>

问题是由于ng斗篷,element.node().parentNode.clientHeight;为0。有没有办法将链接功能推迟到移除ng斗篷之后?

更新

根据反馈,我尝试了这个。。。

compile: function compile() {
        return {
            pre: function preLink($scope, e) {
                var element = d3.select(e[0]);
                $timeout(function() {
                    var height = element.node().parentNode.clientHeight;
                    element
                        .style({
                            "border": "7px solid black",
                            "min-height": height + "px",
                            "background-image": "url('http://s3.amazonaws.com/dostuff-production/property_assets/23107/yellow-stripes.png')"
                        });
                    element.select(".board")
                        .style("transform", function (d) {
                            var ch = d3.select(this).node().clientHeight;
                            return "translate(" + 0 + "px, " + (height / 2 - ch / 2) + "px)"
                        })
                })
            }
        }
  }

然而,var height = element.node().parentNode.clientHeight;仍然是0,移除ng斗篷可以修复它

$timeout或其他什么行得通吗?

是的,应该。还要注意,您正在检查pre链接内部的高度,该链接在编译指令之前运行。在编译之前,它将没有内容

您应该将代码移动到post链接,并可能在那里使用$timeout,这取决于jg-body模板的作用。此外,如果它依赖于异步数据来渲染并获得高度,则需要处理另一个问题,即需要显示更多代码来排序

最新更新