如何等待在一个角块运行的承诺? [英] How to wait for a promise in a Run block in Angular?

查看:115
本文介绍了如何等待在一个角块运行的承诺?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的需要自举角之前加载一些数据。为此,我已经创建了一个运行块/在这个运行块,我呼吁其负载的服务的方法从服务器的一些数据。由于这是一个异步调用,JavaScript和执行,这会导致一些不确定的对象在我的应用程序进一步得到。

我与路线的工作,但它是不符合解析物业工作的一个选项。

有人可以提供关于如何等待一个承诺要在运行准备就绪的解决方案阻止?

编辑:

由于给定的答案,我已经通过手动自举角度解决它:

  $(文件)。就绪(函数(){
    $阿贾克斯('/数据/ GetDropdownData',{类型:'POST'})。完成(功能(响应){
        app.run(['ListService',函数(ListService){
            ListService.setData(响应);
        }])        angular.bootstrap(文件,[SMA]);
    })
})


解决方案

由于我评论会把在这里

您可以尝试手动引导

 <脚本>
   angular.element(文件)。就绪(函数(){
      angular.bootstrap(文件);
   });
< / SCRIPT>

在这种情况下,你不能使用控制器和获取数据的角度的方式引起。我认为你将不得不通过jQuery来加载数据,引导你的角度jQuery的回调,然后用你的jQuery得到的数据初始化的变量范围。

I really need some data to be loaded before bootstrapping angular. For this reason I've created a run block/ Within this run block I call a method on a service which load some data from the server. Since this is a async call, JavaScript gets further with execution, which results in some undefined objects in my app.

I am working with routes, but it is not an option to work with the resolve property.

Could someone provide a solution on how to wait for a promise to be ready in the run block?

EDIT:

Thanks to the given answers, I have solved it by manually bootstrapping angular:

$(document).ready(function () {
    $.ajax('/Data/GetDropdownData', {type:'POST'}).done(function (response) {
        app.run(['ListService',  function (ListService) {
            ListService.setData(response);
        }])

        angular.bootstrap(document, ["sma"]);
    })
})

解决方案

As I commented would put it here

You may try manual bootstrap

<script> 
   angular.element(document).ready(function() {
      angular.bootstrap(document); 
   });
</script>

Of cause in this case you cannot use controllers and angular way of getting data. I think you would have to load your data by jQuery, bootstrap your angular in jquery callback and then init your scope variable with data you got from jquery.

这篇关于如何等待在一个角块运行的承诺?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆