AngularJS函数执行顺序问题
我正在使用调用函数Save_Click in Angular controller on button Click 以下是执行顺序AngularJS函数执行顺序问题,angularjs,visual-studio,Angularjs,Visual Studio,我正在使用调用函数Save_Click in Angular controller on button Click 以下是执行顺序 Line Code 1 Declare a variable V1 2 console.log(V1) 3 Perform a server post back, and assign a value to V1 depending on the values the service returns. console.log(V1) 4 co
Line Code
1 Declare a variable V1
2 console.log(V1)
3 Perform a server post back, and assign a value to V1 depending on the values the service returns. console.log(V1)
4 console.log(V1)
我看到的是-在控制台中#4在#3之前打印
此外#3中指定的值不会打印在#4中
原因可能是什么?我做错了什么?
如果我也使用$localStorage(ngStorage)库,我会遇到同样的问题
注意-我使用Visual Studio/.Net解决方案在Index.cshtml中托管angular应用程序。使用下面的方法
1 Declare a variable V1
2 console.log(V1)
3 async (Perform a server post back, and assign a value to V1 depending on the values the service returns. await console.log(V1))
4 console.log(V1)
现在#3将在#4之前打印这是异步代码的自然行为。一些代码块(如http服务)事件是异步的。当异步代码块达到#3时,在您的情况下,程序将调用该语句并继续执行下一个语句,异步代码的代码将在完成异步操作后执行。这就是为什么#4(非异步)在#3(异步代码)之前先启动的原因。如何确保先执行异步代码?为此,需要中断异步行为。ES6提供了
async
和wait
功能。wait将等待操作完成。请确保await仅在异步函数中工作,使用以下命令,服务函数似乎不会进行更改。CallAPI:async函数(ServiceURL,objectToSerialize){var url=window.location.protocol+“/”+window.location.hostname+”:94/api/Message/“+ServiceURL+”/”;console.log(url);var-req={method:'PUT',url:url,data:objectToSerialize};return wait$http(req);}