angular.js - angularjs顺序问题?
曾经蜡笔没有小新
曾经蜡笔没有小新 2017-05-15 17:00:54
0
4
583

1、我在app.run()里面通过ajax获取用户信息,在controller里需要用到获取到的用户信息,但是我发现有时我controller里运行的时候,app.run()里面获取用户信息的ajax还没返回,这就导致了报错。请问有没有好的解决办法,感觉遇到一大堆ajax时顺序好乱?

曾经蜡笔没有小新
曾经蜡笔没有小新

reply all(4)
迷茫

Why should the request data be placed in run? js must pay attention to asynchronous issues.

  • You can request data through ajax in the controller (it is recommended to use $http). Before the data comes back, put a loading circle on the page. After the data comes, it will be refreshed to the page and the loading will be hidden.

  • You can also use the resolve attribute of the route. This is to wait until the data is obtained before loading the page. Please refer to the routing tutorial for specific usage.

Both methods are very easy to use, I personally like the first one.

过去多啦不再A梦

Resovle in the router

刘奇
  1. You can get the promise requested by ajax during run in the controller, and then write the code in the then callback.

  2. You can also watch the user information you want in the controller, and then execute your logic when there is value.

Peter_Zhu

You can define a variable ok after the run ajax is completed to identify whether success
In the controller
function waitOK() {

        if (ok) {
            setTimeout(function() {
                waitOK();
            }, 100);
        } else {
            //todo....
        }

}

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template