Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular use lodash in HTML template

Tags:

angular

lodash

Please, could someone tell me how to use Lodash in an Angular HTML template file?

I know how to use it in a component's TypeScript file but not in the component's HTML file.

Thanks

PS This is NOT a dupe of AngularJs: Have Underscore/Lodash/_ in the view template because that relates to AngularJS not Angular.

like image 475
danday74 Avatar asked May 12 '26 06:05

danday74


2 Answers

If you import the module in your TS file, you can assign it to a property of the class and use it in your HTML without any problem.

import * as _ from 'lodash';
...
export class YourClass {
  lodash = _;
  ...
}

<p> {{lodash.isEqual(1,1)}} </p>
like image 143
bugs Avatar answered May 13 '26 20:05

bugs


import * as _ from 'lodash' 

Put _ in a variable and use it in the template is a solution. But if you only want to use only one function from lodash maybe you can have something like

import { isEqual } from 'lodash';

And then put the isEmpty lodash function in a variable instead of the whole lodash library.

like image 33
Anto Avatar answered May 13 '26 19:05

Anto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!