1、我在app.run()里面通过ajax获取用户信息,在controller里需要用到获取到的用户信息,但是我发现有时我controller里运行的时候,app.run()里面获取用户信息的ajax还没返回,这就导致了报错。请问有没有好的解决办法,感觉遇到一大堆ajax时顺序好乱?
迷茫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.
怪我咯2017-05-15 17:02:54
You can get the promise requested by ajax during run in the controller, and then write the code in the then callback.
You can also watch the user information you want in the controller, and then execute your logic when there is value.
天蓬老师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....
}
}