angular-promise

AngularJS : Initialize service with asynchronous data

廉价感情. 提交于 2019-11-25 22:07:57
问题 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

Is this a “Deferred Antipattern”?

泄露秘密 提交于 2019-11-25 21:43:59
问题 I\'m finding it hard to understand the \"deferred antipattern\". I think I understand it in principal but I haven\'t seen a super simple example of what a service, with a differed promise and one with antipattern, so I figured I\'d try and make my own but seeing as how I\'m not super in the know about it I\'d get some clarification first. I have the below in a factory (SomeFactory): //url = \'data.json\'; return { getData: function(){ var deferred = $q.defer(); $http.get(destinationFactory