search

Home  >  Q&A  >  body text

angular.js - angularjs order problem?

1. I obtain user information through ajax in app.run(). I need to use the obtained user information in the controller, but I find that sometimes when I run it in the controller, I get the user information in app.run(). The ajax information has not been returned yet, which resulted in an error. Is there any good solution? It feels like the order is messed up when encountering a lot of ajax?

曾经蜡笔没有小新曾经蜡笔没有小新2828 days ago632

reply all(4)I'll reply

  • 迷茫

    迷茫2017-05-15 17:02:54

    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.

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-15 17:02:54

    Resovle in the router

    reply
    0
  • 怪我咯

    怪我咯2017-05-15 17:02:54

    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.

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-15 17:02:54

    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....
            }

    }

    reply
    0
  • Cancelreply