![](/img/trans.png)
[英]Angular - Using custom directive to load several components is returning undefined
[英]How to load a script using custom angular directive?
我試圖在我的自定義角度指令中加載腳本。 現在,它只是在DOM中添加腳本標記,但它沒有加載它。
angular.module('myApp')
.directive('rjHeader', function () {
return {
restrict: 'E',
template : function(e,a){
return '<script type="text/javascript" charset="utf-8" src="https://my.web.com/path/' + a.locale + '/some.js" ></script>';
}
};
});
並在HTML中
<div rj-Header locale="en_US"></div>
在瀏覽器中打開頁面時,它會正確添加目標腳本。
<script type="text/javascript" charset="utf-8" src="https://my.web.com/path/en_US/some.js" ></script>
但這實際上並沒有加載這個.js文件。 如果我簡單地將此行粘貼到我的html,那么它按預期工作。 如何使用自定義角度指令注入腳本?
這里看起來有點不對勁,但是如果你真的想要這樣做,你可以在你的指令元素上附加一個腳本標簽:
app.directive('loadDirective', function() {
return {
restrict: 'E',
link: function($scope, $el) {
var script = document.createElement('script');
script.src = '//cdnjs.cloudflare.com/ajax/libs/emojione/1.3.0/lib/js/emojione.min.js'
$el.append(script);
}
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.