如何有条件地在 Angular 中包含脚本元素 [英] How to conditionally include script element in Angular
本文介绍了如何有条件地在 Angular 中包含脚本元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试有条件地在我的页面上包含聊天内容:
I'm trying to conditionally include a chat on my page:
<body ng-app="myApp">
<!-- some code -->
<script type="text/javascript" ng-if="expression">
window.$zopim||(function(d,s){var z=$zopim=function(c){z._.push(c)},$=z.s=
d.createElement(s),e=d.getElementsByTagName(s)[0];z.set=function(o){z.set.
_.push(o)};z._=[];z.set._=[];$.async=!0;$.setAttribute("charset","utf-8");
$.src="//v2.zopim.com/?my-zopim-id";z.t=+new Date;$.
type="text/javascript";e.parentNode.insertBefore($,e)})(document,"script");
</script>
</body>
但似乎聊天脚本在 ng-if
指令之前执行.
but it seems that the chat script gets executed before ng-if
directive.
如何让我的 Angular 应用程序先检查条件,然后从 标签执行脚本?
How can I make my Angular app to check the condition first and then execute the script from <script>
tag?
推荐答案
内联 Javascript 将始终优先于 Angular(由浏览器首先解析).最好的方法是使用条件或创建指令在控制器中注入/执行 javascript.
Inline Javascript will always have priority over Angular (parsed first by the browser). The best way is to inject/execute the javascript inside your controller with a condition or creating a directive.
这篇关于如何有条件地在 Angular 中包含脚本元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文