I have an idea for my AngularJS app and I'm curious if the AngularJS community would consider it okay to do it this way. In short, I am connecting to a data API and displaying my results on the page.
I have created an AngularJS service that creates a data store on $rootScope.DataStore
. I also have a service method that updates the DataStore with the data returned from an API endpoint. If I request the "products" API endpoint from inside my controller with DataStore.update('products')
, this would update $rootScope.DataStore.products
with my product data.
Now, in the view/partial, all I need to do is say ng-repeat="product in DataStore.products"
to show my data, and it doesn't matter what controller scope I am in. So, in essence my DataStore is my single source of truth.
What I feel like I gain from this method is easy to follow semantics and minimal controller coding. So, anytime the DataStore is updated, anything that's bound to DataStore also gets updated.
Would this put too much load on the $rootScope
digest cycle, or is this just an odd way to do it? Or is it a totally awesome way? :) Any comments are welcome.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…