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

228
Views
How can I call a function defined in an Angular component in a JavaScript file?

In a .ts file, I have the following code

export class SomeClass {
    .
    .
    .
    public someMethod() {
        ...
    }
}

I want to call this someMethod method in a .js file.

I tried

import { SomeClass } from '../../../../SomeClass';
.
.
.
SomeClass.someMethod();

but got the following error:

someMethod is not a function

Do I have to make the method static?

I prefer not because it calls other methods and I will have to make all of them static.

Can I create an instance of this class in the .js file?

Thanks!

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

possible way

u can build your ts files using build command and convert it to javascript then call it in your js file

this should work

about 4 years ago · Juan Pablo Isaza Report

0

there are two solutions either create instance of the class first then use public method like

let class = new SomeClass();
class.someMethod();

or make it static Method then your code will compile successfully

about 4 years ago · Juan Pablo Isaza Report

0

Futhermore create a instance of the class, you can also use some like

export function MyClass() {
    return new SomeClass()
}

Or if you neeed a constructor

export function MyClass(args) {
    return new SomeClass(args)
}
class SomeClass{
   constructor(arg){this._arg=arg}
}

You can in .ts

import { MyClass} from '...';

@Component({
   ...
})
export class AppComponent {
  MyClass=MyClass //<--if you want to use in .html
  ....
  myfunction(){
     MyClass.someMethod();
     //or
     const obj=MyClass();
     obj.someMethod();
  }
}

You can also, if only defined functions use a const

export const Utils{
    someFunction:()=>{console.log("hello"}
}

And then you use simply Utils.someFunction()

about 4 years ago · Juan Pablo Isaza 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!