gpt4 book ai didi

javascript - 如何使用 Angular 有条件地将资源包含在 中

转载 作者:行者123 更新时间:2023-12-03 08:42:56 24 4
gpt4 key购买 nike

我正在构建一个单页 Angular 应用程序。其中一个 View 的内容依赖于外部 JavaScript 库。

我不想在网站的每个 View 上都包含此外部 JS 资源,而只想在依赖于它的单个 View 上包含该外部 JS 资源。

根据当前 View 有条件地包含代码块的最佳方法是什么?

如果可能的话,我希望我可以放置这样的东西:

<script ng-if="view == 'view1'" type='text/javascript' src='http://webplayer.unity3d.com/download_webplayer-3.x/3.0/uo/UnityObject2.js'></script>

最佳答案

因此您应该在页面中包含库脚本。

然后在绑定(bind)到它需要操作的元素的指令中进行初始化。

app.directive('unity', function () {
return {
link: function (scope, element, attrs) {
// element is jQuery object when jQuery.js is included in page
// before angular - or it is a jQLite object similar to a jQuery object

// config code

u.initPlugin(element[0], "web_ovar_beta.unity3d");
}
}
});

View 中的用法:

<div unity></div>

这可以轻松扩展以将属性从 Controller 传递到指令

关于javascript - 如何使用 Angular 有条件地将资源包含在 <head> 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33003083/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com