angularjs指令模板vs templateURL渲染时间

angularjs directive template vs templateURL rendering timing

本文关键字:时间 templateURL vs 指令 angularjs      更新时间:2023-09-26

我有一个指令,加载一个外部HTML文件作为模板使用"templateURL"。控制器中有一个函数依赖于完全呈现的DOM才能正确执行。当调用该函数时,该函数抛出一个错误,因为预期的DOM元素还没有准备好。

JS:

app.directive('directiveName',function(){
    return{
        restrict:'A',
        replace: true,
        template: '<div id="elmID"></div>',  //** THIS ONE WORKS **//
        templateURL:"template.html",        //** THIS ONE THROWS AN ERROR **//
        scope:false,
        link: function(scope, elm, attrs){},
        controller:function($scope){
            console.log(document.getElementById('elmID'));
        }
});

模板:template.html

<div id="elmID"></div>

实际上,我的template.html文件非常大,所以我不想使用内联代码。有人知道怎么做吗?

你的代码崩溃了,因为它是templateUrl而不是templateURL

要回答您关于渲染/计时的问题,请阅读这篇文章。

基本上顺序是:

  1. 控制器
  2. pre-link
  3. 后链接(又名链接)