admin管理员组文章数量:1415111
I want to include the Telegram login widget in my Angular application. For this, you have to include the following script:
<script async src=".js?5"
data-telegram-login="bot_name" data-size="large"
data-auth-url="/api/telegram"
data-request-access="write"></script>
Embedding scripts in Angular templates is not allowed, it will just be removed. (However, it is possible to include a script tag via this hack.)
Is there a non-hacky way of including this widget?
I want to include the Telegram login widget in my Angular application. For this, you have to include the following script:
<script async src="https://telegram/js/telegram-widget.js?5"
data-telegram-login="bot_name" data-size="large"
data-auth-url="https://myurl.example/api/telegram"
data-request-access="write"></script>
Embedding scripts in Angular templates is not allowed, it will just be removed. (However, it is possible to include a script tag via this hack.)
Is there a non-hacky way of including this widget?
Share Improve this question edited Jun 1, 2019 at 16:06 Kim Kern asked Jun 1, 2019 at 14:17 Kim KernKim Kern 60.7k20 gold badges218 silver badges214 bronze badges2 Answers
Reset to default 4I created the following ponent for the Telegram login widget:
The ponent creates the script tag dynamically and adds the callback function loginViaTelegram(user)
:
@Component({
selector: 'app-telegram-login-widget',
template: `
<div #script style.display="none">
<ng-content></ng-content>
</div>`,
styleUrls: ['./telegram-login-widget.ponent.css']
})
export class TelegramLoginWidgetComponent implements AfterViewInit {
@ViewChild('script', {static: true}) script: ElementRef;
convertToScript() {
const element = this.script.nativeElement;
const script = document.createElement('script');
script.src = 'https://telegram/js/telegram-widget.js?5';
script.setAttribute('data-telegram-login', environment.telegramBotName);
script.setAttribute('data-size', 'large');
// Callback function in global scope
script.setAttribute('data-onauth', 'loginViaTelegram(user)');
script.setAttribute('data-request-access', 'write');
element.parentElement.replaceChild(script, element);
}
ngAfterViewInit() {
this.convertToScript();
}
}
I added the callback function loginViaTelegram
to the window
object (global space) in a dedicated service:
@Injectable({
providedIn: 'root'
})
export class TelegramLoginService {
init() {
window['loginViaTelegram'] = loginData => this.loginViaTelegram(loginData);
}
private loginViaTelegram(loginData: TelegramLoginData) {
// If the login should trigger view changes, run it within the NgZone.
this.ngZone.run(() => process(loginRequest));
}
}
Not rendering script
tags in template code is indeed a design choice from Angular team.
The way to do it is thus:
- Add your scripts to index.html - only relevant if it makes sense to load the script globally.
- Or Add your script programmatically. The post you refer to can be a solution, but brings in additional plication because it takes the input data from template code. A shorter solution is given here, when you're happy with inputing the parameters from the code-side (and the full issue is instructive too): Github issue #4903. Warning though: adding to the
head
is not suitable unless you remove it inOnDestroy
. Prefer adding in the right DOM element of your ponent.
本文标签: javascriptHow can I include the Telegram login widget in an Angular appStack Overflow
版权声明:本文标题:javascript - How can I include the Telegram login widget in an Angular app? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745223866a2648517.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论