2016-02-04 27 views
0

我有这个指令来计算divs的高度。在编译angularJS之后是否可以加载自定义指令?

// Directive to log a height. We have to add on some value to a final height, because css style will be undefined. 
rootApp.directive('logHeightCategories', ['$timeout', function($timeout) { 
    return { 
     restrict: 'A', 
     link: function(scope, element) { 
      scope.prepMaxHeightCat = element.prop('offsetHeight'); 
      scope.value = 0; 
      $timeout(function() { 
       scope.maxHeightCat = scope.value + scope.prepMaxHeightCat; 
      }, 0); 
     } 
    }; 
}]); 

的CSS规则max-height: {{maxHeightCat}}px被应用到该div动画目的。然而,闪烁效应破坏了欲望目标。所以我已经为该div添加了ng-cloakdisplay:none

我认为ng-cloak被渲染为最后,因此我认为我的指令logHeightCategories之前执行。所以我总是得到0px的结果。

...即使我已经添加了一个$timeout函数,希望我能克服执行时间,但我认为情况并非如此。

回答

1

您可以尝试

rootApp.directive('logHeightCategories', ['$timeout', '$compile', 
 
    function($timeout,$compile) { 
 
    return { 
 
     restrict: 'A', 
 
     link: function(scope, element, attrs) { 
 
     scope.prepMaxHeightCat = element.prop('offsetHeight'); 
 
     scope.value = 0; 
 
     scope.maxHeightCat = scope.value + scope.prepMaxHeightCat;  
 
     scope.$watch(attrs.logHeightCategories, function(html) { 
 
      element.html(html); 
 
      $compile(element.contents())(scope); 
 
     }); 
 
     } 
 
    }; 
 
    } 
 
]);

+0

我申请与范围'prepMaxHeightCat'股利,因为我看到你排除了数学,但无论如何,我得到了'0px'输出。 –

+0

我错过了那条线。添加。不需要'$ .timeout',你可以在'attrs'上放置'scope。$ watch',当它改变完成时''compile'' scope'就是想法。 –

+0

控制台日志中还有'ele未定义'的referenceError。我已经在angularjs文件中检查了'ng-cloak'指令,它带有'priority:-10'。我现在不知道,这里的优先是否起到任何作用。 –

相关问题