在 AngularJs 中管理同步 XHR/Ajax 调用
Managing synchronous XHR/Ajax call in AngularJs
我有一个内置的产品 angularJS ,我已经创建了将近 300 个控制器。所有控制器都有一个 XHR 调用(不是 $http 调用,它是基本的 javascript XMLHttpRequest )用于检查会话数据(通过在控制器开头调用服务)。在开始时我犯了一个错误,以同步方式创建它(同步调用在服务中定义),现在它在 mozilla firefox 中已被弃用,并且很快就会在 chrome 中被弃用。有没有其他方法可以在不将所有控制器更改为 $watch 的情况下进行同步 XHR 调用?
var request = new XMLHttpRequest();
request.open('POST', 'path/to/my/APIservice', false);
感谢您的帮助。
感谢@Tom Jenkins 的热心回复,
我已经找到解决方案。
我创建了一个新状态,并从所有其他状态路由到该状态,然后将从新创建的状态异步加载 XHR 调用 state/controller,在获得响应后,我将状态重定向到上一个加载了数据。
我不知道这是否是实际的解决方案,但它节省了我编辑所有 300 个 js 文件的时间。 :)
Eg:
I am refreshing the home state, but it needs some session data to
be loaded from database to show the page. Then I redirect the state to
checkSession state and in that controller I call the service and in the promise of that call I stores the data to $rootScope and then
using $state.go() I redirect the state to home again.
我有一个内置的产品 angularJS ,我已经创建了将近 300 个控制器。所有控制器都有一个 XHR 调用(不是 $http 调用,它是基本的 javascript XMLHttpRequest )用于检查会话数据(通过在控制器开头调用服务)。在开始时我犯了一个错误,以同步方式创建它(同步调用在服务中定义),现在它在 mozilla firefox 中已被弃用,并且很快就会在 chrome 中被弃用。有没有其他方法可以在不将所有控制器更改为 $watch 的情况下进行同步 XHR 调用?
var request = new XMLHttpRequest();
request.open('POST', 'path/to/my/APIservice', false);
感谢您的帮助。
感谢@Tom Jenkins 的热心回复, 我已经找到解决方案。
我创建了一个新状态,并从所有其他状态路由到该状态,然后将从新创建的状态异步加载 XHR 调用 state/controller,在获得响应后,我将状态重定向到上一个加载了数据。
我不知道这是否是实际的解决方案,但它节省了我编辑所有 300 个 js 文件的时间。 :)
Eg:
I am refreshing the home state, but it needs some session data to be loaded from database to show the page. Then I redirect the state to checkSession state and in that controller I call the service and in the promise of that call I stores the data to $rootScope and then using $state.go() I redirect the state to home again.