简体   繁体   English

如何从另一个指令访问指令的DOM元素?

[英]How to access DOM element of directive from another directive?

I'm trying to access DOM element in Directive's link function. 我正在尝试在Directive的链接函数中访问DOM元素。 The element is located in the view of another directive. 该元素位于另一个指令的视图中。 Here is the code: 这是代码:

first directive 第一指令

    (function () {
        'use strict';
        angular.module("testAPP",[])


        .directive('firstDirective', function(){
            var directive = {
                 restrict: 'E',
                 templateUrl: 'firstDirective.html'
                }
                return directive;
        })
  })();

second directive 第二条指令

(function () {
      'use strict';
       angular.module("testAPP",[])
        .directive('anotherDirective', function(){
             var directive = {
                restrict: 'E',
                templateUrl: 'anotherDirective.html',
                link: function($scope){
                  //element from another directive's view

                  var height = document.getElementByClassName("sky")[0].offsetHeight;
                }
             };
             return directive;

           });
     })();

There is a console error saying that height variable is undefined. 出现控制台错误,提示高度变量未定义。 I tried timeout function and that worked for me, but i think it's not a good solution: 我尝试了超时功能,对我有用,但是我认为这不是一个好的解决方案:

setTimeout(function(){
  var height = document.getElementByClassName("sky")[0].offsetHeight;
  console.log(height);
});

I also tried " require ", but it caused an error that the directive can't be found (i think this might be because that directives are located in separate directories) 我也尝试过“ require ”,但它导致找不到指令的错误(我认为这可能是因为指令位于单独的目录中)

So, could you tell me the reason why require does not work, and suggest me better solution than timeout 因此,您能告诉我为什么要求不起作用的原因,并建议我比超时更好的解决方案

首先,这似乎不是一个好主意,而且会被认为是“不好的做法”-您还必须更改指令的优先级,以使其能够按需要的顺序进行编译进行编译,以确保第二个指令尝试访问元素时第一个指令已准备就绪。

I think, your content directive load before your 2nd directive, Add ng-if on your content directive may fix the issue 我认为,您的内容指令在第二个指令之前加载,在内容指令上添加ng-if可能会解决此问题

    angular.module("testAPP",[])
                .directive('secondDirective', function(){
       var directive = {
       restrict: 'E',
       templateUrl: 'secondDirective.html',
       link: function($scope){
           $scope.scondDirctiveLoaded = true;
       }
     };
     return directive;
    });

<second-directive></second-directive>
<div ng-if="scondDirctiveLoaded">
    <content-directive  ng-if="scondDirctiveLoaded" ></content-directive>
</div>

This error happens as the directive is instantiated before the DOM is actually ready. 在DOM实际就绪之前实例化该指令时会发生此错误。 $timeout works as it delays the grabbing of the element until Angulars next tick cycle - while this feels like an anti-pattern it seems to be an acceptable solution to the problem. $ timeout的工作原理是将元素的获取延迟到Angulars下一个刻度周期为止-虽然这感觉像是一种反模式,但似乎可以解决该问题。

There is already an answer to a question similar to this. 与此类似的问题已经有了答案。

How can I run a directive after the dom has finished rendering? dom完成渲染后如何运行指令?

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

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