1

I see "_" being as to scope and rootscope when people try to explain Lodash being used and angular (and its already asked here) and I still don't understand why or what that is really sure what that's doing and where it's appropriate I.e. in a controller or a service ect. because I see it being used differently than something that you would attach to the scope i.e. using it to concatenate lodash to a variable name to do lodash operations with. Can someone give me a very simple and basic explanation to what this is doing and where it's appropriate/what it would look like if you didn't versus did use it?

Community
  • 1
  • 1
garrettmac
  • 8,417
  • 3
  • 41
  • 60
  • Someone asked this question before http://stackoverflow.com/questions/23862119/how-to-make-lodash-work-with-angular-js – ssuperczynski Dec 06 '15 at 08:49
  • This was the question I'm actually referring to I would still like some more exclamation on it – garrettmac Dec 06 '15 at 08:50
  • You create a reference to _ in the $scope so you can use lodash inside your templates for example, like in the linked question by infaustus. – Shanoor Dec 06 '15 at 09:07

2 Answers2

4

It's probably to be able to use Lodash capabilities in the view through the use of $scope.

Although, this is a bad pattern to get into.

It's better to define functionality & logic like Lodash in the controller/directives itself & pass it onto the view with a $scope.<variable>.

Nick Snick
  • 911
  • 1
  • 9
  • 16
  • Thanks for you response. Why is that better exactly? Wouldn't that cause the program to have more leaks? – garrettmac Dec 06 '15 at 10:02
  • The entire point of controllers, directives, services etc is to have the logic of the program contained within those modules. It won't create leaks since all of the logic, functions etc are being handled inside of the controller and only the necessary information is being passed on to the scope. That's the power of angular. – Nick Snick Dec 06 '15 at 10:05
2

It's done to use loadash in your view, so you can call smth like

<span>{{ _.doSmth(); }}</span>

Alexander Mikhalchenko
  • 4,525
  • 3
  • 32
  • 56