Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.6K
Views
¿Cómo incluyo un archivo de script de JavaScript en Angular y llamo a una función desde ese script?

Tengo un archivo JavaScript llamado abc.js que tiene una función 'pública' llamada xyz() . Quiero llamar a esa función en mi proyecto Angular. ¿Cómo puedo hacer eso?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Consulte los scripts dentro del angular-cli.json ( angular.json cuando se usa angular 6+).

 "scripts": [ "../path" ];

luego agregue en typings.d.ts (cree este archivo en src si aún no existe)

 declare var variableName:any;

Importarlo en su archivo como

 import * as variable from 'variableName';
over 4 years ago · Santiago Trujillo Report

0

Para incluir una biblioteca global, por ejemplo, el archivo jquery.js en la matriz de scripts de angular-cli.json ( angular.json cuando se usa angular 6+):

 "scripts": [ "../node_modules/jquery/dist/jquery.js" ]

Después de esto, reinicie ng serve si ya se inició.

over 4 years ago · Santiago Trujillo Report

0

Agregue un archivo js externo en index.html .

 <script src="./assets/vendors/myjs.js"></script>

Aquí está el archivo myjs.js :

 var myExtObject = (function() { return { func1: function() { alert('function 1 called'); }, func2: function() { alert('function 2 called'); } } })(myExtObject||{}) var webGlObject = (function() { return { init: function() { alert('webGlObject initialized'); } } })(webGlObject||{})

Luego declara que está en un componente como el siguiente

demo.component.ts

 declare var myExtObject: any; declare var webGlObject: any; constructor(){ webGlObject.init(); } callFunction1() { myExtObject.func1(); } callFunction2() { myExtObject.func2(); }

demostración.componente.html

 <div> <p>click below buttons for function call</p> <button (click)="callFunction1()">Call Function 1</button> <button (click)="callFunction2()">Call Function 2</button> </div>

me esta funcionando...

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!