简体   繁体   English

在Angular中的组件中插入JavaScript

[英]Inserting JavaScript inside components in Angular

I would like to insert a <script>...</script> inside a .html component in an Angular project and I was wondering how it can be done. 我想在Angular项目的.html组件中插入一个<script>...</script> ,我想知道它是如何完成的。

It seems like any Javascript I insert into this component is just ignored. 似乎我插入此组件的任何Javascript都被忽略了。

Yeah sure you can add javascript in Angular.You can make one external Javascript file and put all your code inside that file.For example you file is external.js in /app folder.And now you can access that file functions in Angular component.This is the easiest way I found. 是的,你确定你可以在Angular中添加javascript。你可以创建一个外部Javascript文件并将所有代码放在该文件中。例如你的文件是/ app文件夹中的external.js。现在你可以访问Angular组件中的那个文件函数。这是我找到的最简单的方法。 You can use import to include your external file. 您可以使用导入来包含外部文件。 Code for that will be following. 代码将遵循。

import './external.js';
declare var myExtObject: any;

Now use one variable here I named myExtObject to call External.js file function like following any where you want : 现在使用一个变量,我将myExtObject命名为调用External.js文件函数,如下所示:

myExtObject.func1();

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

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