【发布时间】:2019-05-15 21:04:06
【问题描述】:
我在 index.html 中有一些异步 javascript 库的脚本标签,我想通过将 type 属性从“text/plain”更改为“text/javascript”来应用它们一个属性指令来控制它们的“激活”。用户提供 cookie 同意。该指令似乎永远不会被角度到达,也许是因为它在角度区域之外,实际上在构造函数和订阅函数的回调中的日志永远不会在控制台中打印并且指令变得无用。
@Directive({
selector: '[appLoadOnConsent]'
})
export class LoadOnConsentDirective implements OnInit, OnDestroy {
private statusChangeSubscription: Subscription;
constructor(
private ccService: NgcCookieConsentService,
el: ElementRef
) {
// never printed
console.log(el);
}
ngOnInit() {
this.statusChangeSubscription = this.ccService.statusChange$.subscribe(
(event: NgcStatusChangeEvent) => {
// never printed
console.log('cookie status change', event);
});
}
ngOnDestroy() {
this.statusChangeSubscription.unsubscribe();
}
}
在 index.html 中
<head>
....
// library 1
<script type="text/plain" appLoadOnConsent async defer src="https://external.library1.js"></script>
// library 2
<script type="text/plain" appLoadOnConsent async defer src="https://external.library2.js"></script>
</head>
....
我怎样才能达到我的目标?有没有更好的方法来解决我的问题?
【问题讨论】: