This topic created in 4283 days ago, the information mentioned may be changed or developed.
在写angular的时候 要远程请求一个jsonp数据,但是$http.jsonp 只有异步请求,不能改成同步的,导致我 return data 是 undefined。。
问:有没有办法让jsonp 模拟同步请求的方式。
或者有没有 在$http callback return数据,controller 里能等待 callback再渲染 初始化页面。
T-T 或者有 angularjs 请求 jsonp,文章列表与内页 切换的demo(其实就是类似博客页面)github上面换个好几个关键词搜索都没有找到,请原谅我的伸手!!
多谢!
11 replies • 2014-08-22 12:07:54 +08:00
 |
|
1
beaaar Aug 21, 2014 1
不知道我理解的对不对,可以controller里先给页面要铺设的内容定义一个初始化的内容;类似“载入中”,等callback执行完毕,替换掉相应变量即可。
|
 |
|
3
binux Aug 21, 2014
不是 angular, jsonp 也是异步的,你都用 JS 了习惯异步吧
|
 |
|
4
jsonline Aug 21, 2014 via Android
在JS里不要想同步。
|
 |
|
5
loddit Aug 21, 2014
要等什么事情完成再渲染初始化页面。 可以在 $routeProvider.when 里面用 reslove
|
 |
|
6
kimmykuang Aug 21, 2014
list page与detail page的切换,我现在做的hybrid app项目里都是把detail page作为一个层覆盖到list page上的,好像没什么难的嘛
|
 |
|
8
chemzqm Aug 22, 2014 1
promise啊 angular支持的
|
 |
|
9
Arrowing Aug 22, 2014 1
angular里的http不支持同步的,要同步可以使用其他ajax类库,或者使用promise
|
 |
|
10
ekousp Aug 22, 2014
1. service 获取数据返回promise,在controller里 promise.then(...) 2. 使用 nv-view: resolve - {Object.<string, function>=} - An optional map of dependencies which should be injected into the controller. If any of these dependencies are promises, the router will wait for them all to be resolved or one to be rejected before the controller is instantiated. If all the promises are resolved successfully, the values of the resolved promises are injected and $routeChangeSuccess event is fired. If any of the promises are rejected the $routeChangeError event is fired. 3. 页面加载时先获取数据,然后手动启动bootstrap: $.get('http://data').done(function() { angular.boostrap(element, ['myapp']); });
|
 |
|
11
sivacohan Aug 22, 2014 via Android
jsonp 不是有success吗?渲染写成handler在这回调不行吗?
|