0

For the subject matter, I found this SO post: Initialize service with asynchronous data

While this looks good, it is two years old and some of the references are out of date. Ex:

$http.get('url').success replaced with $http.get('url').then

Anyway, I have my model:

app.factory('User', function($http, $q) {
    var myData = null;

    var promise = $http.get('data.json').then(function (data) {
        myData = data;
    });

    return {
        promise: promise,
        setData: function (data) {
            myData = data;
        },
        doStuff: function () {
            return myData.getSomeData();
        }
    };
});

and I need to share it across multiple controllers:

app.controller('controllerOne', function(User) {
    // do stuff
});

app.controller('controllerTwo', function(User) {
    // do stuff
});

app.controller('controllerThree', function(User) {
    // do stuff
});

and each controller called from ngRoute:

app.config(function($routeProvider) {
    $routeProvider
        .when("/one",{,
          controller: "controllerOne"
        })
        .when("/two",{
          controller: "controllerTwo"
        })
        .when("/three",{
          controller: "controllerThree"
        });
    });

... in no particular order

Now, there are a lot of posts that reference angular-deferred-bootstrap, but that library has not been updated since 2015. My question: Is angular-deferred-bootstrap still the preferred method to do this, or is there another / better way?

KingFish
  • 8,773
  • 12
  • 53
  • 81
  • `angular-deferred-bootstrap` is meant to bootstrap apps, it means, make angular apps run, if you need the data before the angularjs app gets started, fine, it works for you. But it looks like you want to load the data before the route I am right? – lenilsondc Oct 08 '18 at 18:19
  • Yep, that is correct – KingFish Oct 08 '18 at 18:23
  • the answer you mentioned suggests that you use the `resolve` on the route definition, which would inject a `Service` into your controller and the router would wait til the promise gets resolved to finally render the route. Does that work for you? – lenilsondc Oct 08 '18 at 18:26
  • The issue with resolve is I would have to define it on every "when" statement, no? – KingFish Oct 08 '18 at 18:55
  • Yes, you would! But do you need to use it in every controller? If so, using ` angular-deferred-bootstrap` seems fit better. – lenilsondc Oct 08 '18 at 19:14

0 Answers0