How to expose method to all my templates?

it is quite handy to have Object.keys() available in my template via objectKeys = Object.keys; in my .ts file.

I can easily output the number of properties in an object (objectKeys(foo).length) or use *ngFor with my objects (*ngFor="let key of objectKeys(foo) ... {{ foo[key] }}).

Right now I copy the objectKeys = Object.keys; line to all my .ts files.
Is there a way to expose this method to all my templates easily?

(Bonus question: Is there a better way to do what I do here? Somehow this feels… dirty :stuck_out_tongue: )

What are you doing with it? I can’t think of a situation where I would want templates doing such low-level things.

1 Like

See next line of my post.

Sorry, but I couldn’t make head or tail out of it.

Besides the concerns @rapropos has…

This sounds like you could use as “Utilities” service that you inject into all your pages.

As these are parameter properties, if you set it to public (e.g. constructor(public util: UtilService) { ... }) you can just use util in your template to access it.