简体   繁体   English

如何将普通的js函数注册到angular中的@Output

[英]how to register plain js function to @Output in angular

I have a function in the script tag in index.html hideAppLoader() and there is an angular app component in index.html. 我在index.html hideAppLoader()script标签中有一个函数,并且index.html中有一个角度应用程序组件。 I want to call that function when the application got bootstrapped. 我想在应用程序启动时调用该函数。 I have tried with @Output but this is not working. 我已经尝试使用@Output但这不起作用。 Here is the code 这是代码

index.html index.html

<head>
  <script>
     function hideAppLoader() { debugger;}
  </script>
</head>
<body>
  <my-app (onAppLoad)="hideAppLoader($event)"></my-app>
</body>

and in the ts file 并在ts文件中

@Output() onAppLoad = new EventEmitter();

ngOnInit() {
  this.onAppLoad.emit();
}

but the function is not getting called even there is no error on the console. 但是即使控制台没有错误,也不会调用该函数。 I have even tried (onAppLoad)="this.hideAppLoader($event)" and (onAppLoad)="window.hideAppLoader($event)" 我什至尝试过(onAppLoad)="this.hideAppLoader($event)"(onAppLoad)="window.hideAppLoader($event)"

What is the solution or is even possible to call plain js function from angular component when it is loaded? 解决方案是什么,甚至在加载角度组件时可以从角度组件调用纯js函数?

Since the script function is global you do not need to use and EventEmitter. 由于脚本功能是全局的,因此您无需使用和EventEmitter。 just change your ngOnInit to be: 只需将您的ngOnInit更改为:

ngOnInit() {
  window.hideAppLoader();
}

Judging by the name of hideAppLoader you could define the loader inside of the my-app tag and Angular will remove it once the AppComponent is Loaded. 根据hideAppLoader的名称判断,您可以在my-app标签内定义加载my-app ,一旦加载了AppComponent,Angular就会将其删除。

<my-app>
  <img src="assets/loader" />
</my-app>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM