Where to save utility functions in angular2, which are available in templates

I have some simple β€œclean” utility functions in my application that I want to keep in one place. The problem is that I can import them into my components using:

import { utilities } from '../shared/utilities' 

However, these utility functions / methods are not available in my template files (because they are associated only with the component class). I need to alias these functions in my component in order to use them. Is there a better way to do this?

+7
angular typescript
source share
1 answer

I think you should use the utilities in the template if you enter it into your controller. Something like that:

 import { Utilities } from './shared/utils'; @Component({ .... .... }) export class ExampleComponent { constructor(public util: Utilities) { } } 

In your template, you can use util.your_function

Update:

 export class ExampleComponent { constructor(public util: Utilities) { } get staticMethod1() { return Utilities.staticMethod1(); } } 
+3
source share

All Articles