Artur Aralin2014-10-05 10:32:20
Artur Aralin, 2014-10-05 10:32:20

AngularJS. How to implement waiting for the completion of promis-s?

Hello! What is the actual problem: I have a User.hasRole(string) method, there is a request to the server to get data about the user

var self = this;
this.userData = null;
$q.all(Auth.currentUser()) /* Внутри происходит запрос */
    .then(function(currentUser) {
      self.userData = currentUser;
      self.isAuth = true;
    },function(error) {
      self.userData = null;
      self.isAuth = false;

In one of the controllers, I pass the hasRole() method to $scope and use it like this
<li ng-if="hasRole('user')">

and everything would be fine, but angular swears that the User.userData == null property.
Question: how to make the hasRole method wait for promises to complete?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
_ _, 2014-10-05

Lay out the full service code and an example of using hasRole.
For app-wide data that is supposed to be preloaded at application startup, I would use the angular deferred bootstrap module.

Sergey, 2014-10-05
Protko @Fesor

I may not understand the essence of the problem correctly, but it seems to me that you want the view to be displayed only after loading the data. In this case, you can apply controller resolvers (if you use ngRoute/uiRouter).
It is also not clear why you use $q.all when there is one promise for everything. Well, they say $q.all expects an array and will pass an array to the arguments

Auth.currentUser().then(function(currentUser) {
    self.currentUser = currentUser;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question