0

Currently i would like to figure out, if it is possible to execute a additional service which executes an asynchronous call and bind the response in the current scope and afterwards bootstrapping my application.

I need this behavior, because i wanted to fetch some data from a server and bind this to the scope. I tried some solutions (like the resolve method in the ui-router component) but i couldn't solve it yet.

In addition i wanted to avoid to use the $watch method to observe the variable.

Anyone have a solution for this problem?

Thanks in advance.

xyNNN
  • 492
  • 3
  • 21
  • 1
    Why not have the controller start a $http and fill in the scope variables on success? – Marvin Smit Oct 26 '14 at 09:06
  • Check the answers to this question http://stackoverflow.com/questions/16286605/initialize-angularjs-service-with-asynchronous-data – Joao Leal Oct 26 '14 at 09:19
  • Thanks for your answers. The problem is that this don't works for directives - anyone have a solution for that? – xyNNN Oct 28 '14 at 15:58

1 Answers1

0

Short of writing your own manual bootstrapping, you can't use a service before angular has been bootstrapped. But why do you need to? You can simply fetch the data after angular has bootstrapped, e.g. like Marvin Smit mentions in the comments, fetch it in your controller:

function MyCtrl($http, $scope) {
    $http.get('whatever')
    .then(function success(response) {
        $scope.stuff = response.data
    })
}

Angular's directives and interpolation are written with this in mind, and would not error if there's no $scope.stuff defined when the controller is executed.

Or you could use ngRoute/ui-router and the resolve parameter for a route(see https://docs.angularjs.org/api/ngRoute/provider/$routeProvider)

Sacho
  • 2,169
  • 1
  • 14
  • 13
  • Yes, this is currently my implementation and worked for almost all cases very well. The problem ist, that i have in several directives a condition of this fetched information and the directives are executed before the asynchronous request is executed. – xyNNN Oct 26 '14 at 09:18
  • But that doesn't really explain why these directives can't have a default behavior if the data is *missing*. Also, you can still use resolve. If you don't want to do that, you can write a "resolver" directive which first waits on the data, and then compiles its template. – Sacho Oct 26 '14 at 09:31
  • Then i will try to explain my intend with more details. I have a simple directive which injects a UserService to retrieve the information about the currentUser like his name or his UserRoles. My directive should load in dependency of his UserRole an separate template. When i inject mit UserService in in the templateUrl function from my directive i ask for UserService.IsUserRoleTypeFoo() then i will get an undefined return because on the execution of the directive the asynchronous call from my UserService.init() method isn't executed. – xyNNN Oct 26 '14 at 10:39
  • And also when i implement a promise in my ApplicationController, the directive loads the template without to wait till the async call is executed. Especially to mention is, that i want to implement a full stateless application, so only i want to save on client side is my bearer token. – xyNNN Oct 26 '14 at 10:39
  • No one have a solution for this intend? – xyNNN Oct 26 '14 at 22:56