推荐关注
› Meteor
› JSLint - a JavaScript code quality tool
› jsFiddle
› D3.js
› WebStorm
推荐书目
› JavaScript 权威指南第 5 版
› Closure: The Definitive Guide
taoche

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

  •  
  •   taoche · Aug 21, 2014 · 5192 views
    This topic created in 4461 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
    beaaar
    beaaar
    Aug 21, 2014❤️1
    1
    不知道我理解的对不对,可以controller里先给页面要铺设的内容定义一个初始化的内容;类似“载入中”,等callback执行完毕,替换掉相应变量即可。
    solodxg
    solodxg
    Aug 21, 2014❤️1
    2
    promise ?
    binux
    binux
    Aug 21, 2014
    3
    不是 angular, jsonp 也是异步的,你都用 JS 了习惯异步吧
    jsonline
    jsonline
    Aug 21, 2014 via Android
    4
    在JS里不要想同步。
    loddit
    loddit
    Aug 21, 2014
    5
    要等什么事情完成再渲染初始化页面。
    可以在 $routeProvider.when 里面用 reslove
    kimmykuang
    kimmykuang
    Aug 21, 2014
    6
    list page与detail page的切换,我现在做的hybrid app项目里都是把detail page作为一个层覆盖到list page上的,好像没什么难的嘛
    NathanInMac
    NathanInMac
    Aug 22, 2014
    7
    @beaaar 你理解是对的。都是 2 way binding 的年代,自动就更新了
    chemzqm
    chemzqm
    Aug 22, 2014❤️1
    8
    promise啊 angular支持的
    Arrowing
    Arrowing
    Aug 22, 2014❤️1
    9
    angular里的http不支持同步的,要同步可以使用其他ajax类库,或者使用promise
    ekousp
    ekousp
    Aug 22, 2014
    10
    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
    sivacohan
    Aug 22, 2014 via Android
    11
    jsonp 不是有success吗?渲染写成handler在这回调不行吗?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1484 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 46ms · UTC 06:05 · PVG 14:05 · LAX 22:05 · JFK 01:05
    ♥ Do have faith in what you're doing.