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

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

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