-1

I want to crate some methods and call them from html of different components. On the other hand, there is some approach as on How to call static method of other class in .html (not in .ts)?, but it is old and I am wondering if there is a better way in Angular 9 to call the method directly. Also it is possible to use a shared service and define shared method in this service, but I am looking another approach to call static methods directly from html. How can I do that?

demoBase.ts:

export class DemoBase {
    static demoMethod(id) {
        //
    }
}

Then I want to call this method from html of another component:

list.component.ts:

import { DemoBase } from '@pages/demoBase';

//other stuff (I do not want to create a method here to access base method)

list.component.html:

<div><span [ngClass]="DemoBase.demoMethod(record.Id)">{{record.Name}}</span></div>

1 Answer 1

0

I believe services in Angular is what you are looking for?

They are shared with their data and methods as well. They can be shared at module level.

Look at this article for details.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for reply, I use that approach in my project. But the situation in my question i a little bit different, the methods are like a calculator that does not fetch data from server and for this reason I wanted to create a new separate class for these methods. On the other hand, even in service methods, I do not want to create the same methods in the components again.
So, I think the best idea is to use @ CalebMacdonaldBlack 's solution on How to call static method of other class in .html (not in .ts)?. Is not it?
Even if they do not fetch data from the server, the services can serve the purpose of shared methods and data. Also, whatever method you go with, you will need to have the declaration of the instance of that class somewhere (in the .ts), without which the HTML won't know about the instance variable. Look at this may be: stackoverflow.com/questions/41857047/…
The approach on the page I mentioned above seems to be cleaner as it uses scoreboardInstance = ScoreBoard.getInstance(); instead of method declaration. Is that true?
@Manish It is completely unrelated answer. Clint Eastwood does not ask data sharing between components.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.