jamiltz
jamiltz

Reputation: 1144

Best practice for angular service

I have a simple question about best practices.

A service loads once at load time.

Let's say there is a method called getUser in the service called user. I have to call getUser in several controllers.

The GET request will happen twice right? Is there a good practice to check whether the data has already been fetched to avoid this second call?

Upvotes: 1

Views: 240

Answers (1)

ms87
ms87

Reputation: 17492

Yes, the call gets executed twice. You can use angular's built in $http cache option, or you could use an existing module like angular-cache, or other libraries such as Breeze or Amplify. You can also try handling it yourself, probably the worst option.

Upvotes: 1

Related Questions