AngularJS : Initialize service with asynchronous data
问题 I have an AngularJS service that I want to initialize with some asynchronous data. Something like this: myModule.service(\'MyService\', function($http) { var myData = null; $http.get(\'data.json\').success(function (data) { myData = data; }); return { setData: function (data) { myData = data; }, doStuff: function () { return myData.getSomeData(); } }; }); Obviously this won\'t work because if something tries to call doStuff() before myData gets back I will get a null pointer exception. As far