javascript Angular js - 解析和 run() 执行顺序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24249081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:27:33  来源:igfitidea点击:

Angular js - resolve and run() order of execution

javascriptangularjsmoduleconfig

提问by itsme

Thanks to this answer AngularJS app.run() documentation?i can see the order in which modules are ran by Angular, my question is:

感谢这个答案AngularJS app.run() 文档?我可以看到 Angular 运行模块的顺序,我的问题是:

if I have:

如果我有:

app.config(function () {
    $routeProvider.when('/', {
        ....
        resolve: {
            // something to resolve
        }
    });
});

app.run(function () {
    // something to run
});

Will run()be executed before the routeProvider resolve:{}is resolved?

run()在routeProviderresolve:{}解析之前执行吗?

回答by KayakDave

At least in my experiments, yes the resolve is run after app.run.

至少在我的实验中,是的,解析是在app.run.

In this jsfiddleyou can see the calling order I got was:

这个 jsfiddle你可以看到我得到的调用顺序是:

  1. app config
  2. app run
  3. directive setup
  4. directive compile
  5. app controller
  6. directive link
  7. ** Data resolve called **
  8. new route's controller
  1. 应用配置
  2. 应用程序运行
  3. 指令设置
  4. 指令编译
  5. 应用控制器
  6. 指令链接
  7. ** 数据解析调用 **
  8. 新路由的控制器

As you can see in the fiddle, I checked this by using a console.logfunction as the value of a property of the object handed to resolve:

正如您在小提琴中看到的,我通过使用一个console.log函数作为传递给的对象的属性值来检查这一点resolve

resolve: {
            data: function() {
              console.log('Data resolve called');
            }
         }

You can use this same approach in your code to check when routeProviderbegins checking the dependencies.

您可以在代码中使用相同的方法来检查何时routeProvider开始检查依赖项。