简体   繁体   English

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

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

I have a angular directive: 我有一个角度指令:

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

The intent is to push the html from themer.html into a main page. 目的是将themer.html中的html推送到主页中。

Right now inside my my_profile.html I have the tag <templater></templater> . 现在,在my_profile.html中,我具有标签<templater></templater>

Now, the html displays perfectly, however the css and js are non-operational. 现在,html可以完美显示,但是css和js无法运行。 THe css tags in the template referenced by the directive affect and are affected by the same js and css files associated with the parent document. 指令引用的模板中的css标签会影响并且受与父文档关联的相同js和css文件的影响。

How do I tell the directive to enforce the rules of the parent file on the inserted file? 如何告诉指令在插入的文件上强制执行父文件的规则?

Thanks 谢谢

You will not load css & js files from the partial view, as the link & script tag will not loaded. 您不会从局部视图中加载cssjs文件,因为不会加载linkscript标签。 Partial will only load the html from it. 部分将仅从中加载html。 If you want to make them working refer this answer 如果您想让他们正常工作,请参考此答案

Side Note 边注

You should $timeout instead of setTimeout , that will ensure your angular element has updated with binding on html. 您应该使用$timeout而不是setTimeout ,这将确保您的angular元素已使用html上的绑定进行了更新。

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

Use $timeout of angular instead of the setTimeOut. 使用$ timeout of angular代替setTimeOut。 Inject the $timeout dependency in directive 将$ timeout依赖项注入指令

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

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