简体   繁体   English

如何使角度等待DWR呼叫响应?

[英]How to make angular wait for response of DWR Call?

function with DWR Call  
self.getUsers = function()
        {
            console.log("Get User called");
             userProjectDWRManager.getAllUsers(function(list){
            console.log(list);
           return list;
        });
      }


  var list = self.getUsers();
console.log(list) //undefined

I tried promise concept but I don't understand exactly how to apply it with DWR exactly. 我尝试了Promise概念,但我不完全了解如何将其正确地应用于DWR。

Edit: 编辑:

The whole scenario is that I am in 整个情况是我在

 $transitions.onStart({
      }, function(trans) {
// get list here from DWR call
    if(list.length==0)
                {
                    event.preventDefaults();
                    $state.go('register',{});
                }

      }

In this function I want to get the list and wait for the list to return before going to the next state. 在此函数中,我要获取列表并等待列表返回,然后再进入下一个状态。 But it always goes to the next state. 但是它总是进入下一个状态。

you can pass a reference to a callback function into promise function to execute after promise resolved. 您可以将对回调函数的引用传递到promise函数中,以在promise解析后执行。

self.getUsers = function(callback)
        {
            console.log("Get User called");
             userProjectDWRManager.getAllUsers(function(list){
             callback(list);//executes the callback function
        });
      }


self.getUsers(showList); //pass reference to callbak function

function showList(list){
  console.log(list);
}

for your updated question 为您更新的问题

        $transitions.onStart({
    }, function(trans) {
        // get list here from DWR call
        userProjectDWRManager.getAllUsers(function(list){
            if(list.length==0)
            {
                event.preventDefaults();
                $state.go('register',{});
            }


            else
                console.log('list is Not empty');
        });
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM