Call static function from angular2 template
Only instance members of the components class can be called from the view.
If you want to call static members, you need to provide a getter in the component.
export class MyComponent {
parseDate = DateService.parseDate;
}
then you can use it like
(input)="event.date=parseDate($event.target.value)"
Gunter's answer is perfectly valid and is the way I've done it most of the time.
If you are using typescript, you additionally have the option of creating a custom decorator to provide functions to your view so your component remains uncluttered.
Example:
Define a decorator:
import {StaticClassFunctions} from "./static-class-functions"
export function CustomDecorator(): Function {
return (target: Function): Function => {
target.prototype.yourStaticMethod = (param1) => {
return StaticClassFunctions.yourStaticMethod(param1);
}
}
}
Apply the decorator to your component:
@Component{ ... }
@CustomDecorator()
export class YourComponent { ... }
Now your have access to those static functions in your view without having to declare them in your Component! Very useful for repetitive "utility" functions to support view formatting and such (like enum casting!)
<span>{{yourStaticMethod(yourInput)}}</span>
You won't have access in your component though unless you declare the function at the top so it can compile.