V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
taoche
V2EX  ›  JavaScript

请教一个关于 angular 关于 jsonp 的问题

  •  
  •   taoche · 2014-08-21 17:33:15 +08:00 · 4201 次点击
    这是一个创建于 3543 天前的主题,其中的信息可能已经有所发展或是发生改变。
    在写angular的时候 要远程请求一个jsonp数据,但是$http.jsonp 只有异步请求,不能改成同步的,导致我 return data 是 undefined。。

    问:有没有办法让jsonp 模拟同步请求的方式。
    或者有没有 在$http callback return数据,controller 里能等待 callback再渲染 初始化页面。

    T-T 或者有 angularjs 请求 jsonp,文章列表与内页 切换的demo(其实就是类似博客页面)github上面换个好几个关键词搜索都没有找到,请原谅我的伸手!!

    多谢!
    11 条回复    2014-08-22 12:07:54 +08:00
    beaaar
        1
    beaaar  
       2014-08-21 17:44:52 +08:00   ❤️ 1
    不知道我理解的对不对,可以controller里先给页面要铺设的内容定义一个初始化的内容;类似“载入中”,等callback执行完毕,替换掉相应变量即可。
    solodxg
        2
    solodxg  
       2014-08-21 20:01:40 +08:00   ❤️ 1
    promise ?
    binux
        3
    binux  
       2014-08-21 20:18:53 +08:00
    不是 angular, jsonp 也是异步的,你都用 JS 了习惯异步吧
    jsonline
        4
    jsonline  
       2014-08-21 20:36:15 +08:00 via Android
    在JS里不要想同步。
    loddit
        5
    loddit  
       2014-08-21 21:23:22 +08:00
    要等什么事情完成再渲染初始化页面。
    可以在 $routeProvider.when 里面用 reslove
    kimmykuang
        6
    kimmykuang  
       2014-08-21 22:21:20 +08:00
    list page与detail page的切换,我现在做的hybrid app项目里都是把detail page作为一个层覆盖到list page上的,好像没什么难的嘛
    NathanInMac
        7
    NathanInMac  
       2014-08-22 00:13:03 +08:00
    @beaaar 你理解是对的。都是 2 way binding 的年代,自动就更新了
    chemzqm
        8
    chemzqm  
       2014-08-22 01:46:43 +08:00   ❤️ 1
    promise啊 angular支持的
    Arrowing
        9
    Arrowing  
       2014-08-22 09:11:52 +08:00   ❤️ 1
    angular里的http不支持同步的,要同步可以使用其他ajax类库,或者使用promise
    ekousp
        10
    ekousp  
       2014-08-22 11:52:59 +08:00
    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']);
    });
    sivacohan
        11
    sivacohan  
       2014-08-22 12:07:54 +08:00 via Android
    jsonp 不是有success吗?渲染写成handler在这回调不行吗?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   748 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 21:42 · PVG 05:42 · LAX 14:42 · JFK 17:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.