1

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?

1
  • I wondered the same about here is some helpful info Angular Best Practices Commented May 17, 2014 at 22:41

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! But sometimes the data changes and caching is not the best solution. In this example bit.ly/1o7f1fk, you see that if this.busy is true it doesn't make subsequent calls. Do you think it's a good practice?
@jamiltz , that busy flag is to check whether a $http call is already in progress, if it is then no more data is retrieved. not sure how that relates to your original question, but in this case i think it's being done since multiple scroll events could be firing at the same time.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.