繁体   English   中英

如何在角度指令输出中运行javascript和CSS?

[英]how to run javascript and css in a angular directive output?

我有一个角度指令:

app.directive('templater', function() {
  return{
    restrict: 'E',
    templateUrl: '../../tmpl/themer.html',
    link: function(scope, element, attrs){
       // execute metisMenu plugin
        setTimeout(function(){
            $(element).templater();
        }, 1);
    }
  }
});

目的是将themer.html中的html推送到主页中。

现在,在my_profile.html中,我具有标签<templater></templater>

现在,html可以完美显示,但是css和js无法运行。 指令引用的模板中的css标签会影响并且受与父文档关联的相同js和css文件的影响。

如何告诉指令在插入的文件上强制执行父文件的规则?

谢谢

您不会从局部视图中加载cssjs文件,因为不会加载linkscript标签。 部分将仅从中加载html。 如果您想让他们正常工作,请参考此答案

边注

您应该使用$timeout而不是setTimeout ,这将确保您的angular元素已使用html上的绑定进行了更新。

   // execute metisMenu plugin
    $timeout(function(){
        $(element).templater();
    }, 1);

使用$ timeout of angular代替setTimeOut。 将$ timeout依赖项注入指令

暂无
暂无

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

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