如何在AngularJS的Run块中等待诺言? [英] How to wait for a promise in a Run block in AngularJS?
问题描述
我真的需要在引导角度之前加载一些数据.由于这个原因,我创建了一个run
块/在这个run
块中,我在服务上调用了一个方法,该方法从服务器加载一些数据.由于这是一个异步调用,因此JavaScript可以进一步提高执行力,这会导致我的应用程序中出现一些未定义的对象.
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.
我正在使用路由,但不能使用resolve
属性.
I am working with routes, but it is not an option to work with the resolve
property.
有人可以提供有关如何等待run
块中的诺言的解决方案吗?
Could someone provide a solution on how to wait for a promise to be ready in the run
block?
感谢给出的答案,我已经通过手动引导角度来解决了这个问题:
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"]);
})
})
推荐答案
我评论过将其放在此处
您可以尝试手动引导程序
<script>
angular.element(document).ready(function() {
angular.bootstrap(document);
});
</script>
在这种情况下,您不能使用控制器和角度方式获取数据.我认为您将必须通过jQuery加载数据,在jquery回调中引导角度,然后使用从jquery获得的数据初始化您的作用域变量.
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.
这篇关于如何在AngularJS的Run块中等待诺言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!