这是否承诺看起来是正确的? [英] does this promise look correct?

查看:106
本文介绍了这是否承诺看起来是正确的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎应该是将数据传送到我的范围,但它不是,有什么直接跳出来为错误的低于code?

  angular.module('Lunch.services',[])
  .factoryLunchMates',($ Q $ HTTP) - GT;
    LunchMates =      getLunchMates:() - >
        D = $ q.defer();
        $ http.get('/ lunchers'),然后(响应状态) - GT。
          如果response.status == 200
            d.resolve(response.data)
        返回d.promise    返回LunchMates
angular.module('Lunch.controllers',[])
  .controller('LunchCtrl',($范围,LunchMates) - >
    $ scope.lunchers = LunchMates.getLunchMates()
  )


解决方案

这code: $ scope.lunchers = LunchMates.getLunchMates()设置上的承诺的范围,它依赖于一个古老的德precated功能。

作为的版本> = 1.2 承诺展开是pcated德$ P $,这是的破提交


  

本承诺禁用答应展开,并增加了
  $ parseProvider.unwrapPromises()的getter / setter的API,允许开发人员
  如果需要打开该功能重新开启。 展开承诺将支持
  在日后的角被删除
此设置只允许
  在过渡期间启用它。


  
  

..........


  
  前pression过程中前pression随处可见

previously承诺
  评价将评估为不确定的,而没有解决,到
  如果满足履约价值。


  
  

..........


  
  

重大更改:$分析和一般的模板将不再
  自动解开承诺
。此功能已被德precated和
  如果绝对必要,它可以在过渡期内重新启用
  通过 $ parseProvider.unwrapPromises(真) API


您仍可以使用 $ parseProvider 像这样:

  angular.module('Lunch.controllers',[])
  的.config(($ parseProvider) - GT;
    $ parseProvider.unwrapPromises(真)
    $ parseProvider.logPromiseWarnings(假)
  )

但是,这将在未来的版本中突破(如上所述),所以不是做到这一点:

  angular.module('Lunch.controllers',[])
  .controller('LunchCtrl',($范围,LunchMates) - >
    LunchMates.getLunchMates()然后(数据) - 过夜。;
      $ scope.lunchers =数据
  )

此问题(其中一些人)是很常见的,主要是因为很多的教程和放大器;图书新的开发人员发现所有网站,在1.2版本之前写的,因此没有及时更新。始终保持自己了最新的 https://github.com /angular/angular.js/blob/master/CHANGELOG.md

this seems like it should be delivering data to my scope, but it isn't, is there anything that directly jumps out as wrong with the below code ?

angular.module('Lunch.services', [])
  .factory 'LunchMates', ($q, $http) ->
    LunchMates = 

      getLunchMates: () ->
        d = $q.defer(); 
        $http.get('/lunchers').then (response, status) ->
          if response.status == 200
            d.resolve(response.data)
        return d.promise

    return LunchMates


angular.module('Lunch.controllers', [])
  .controller('LunchCtrl', ($scope, LunchMates) ->
    $scope.lunchers = LunchMates.getLunchMates()
  )

解决方案

This code: $scope.lunchers = LunchMates.getLunchMates() sets a promise on the scope, it relies on an old deprecated functionality.

As of version >=1.2, promise unwrapping is deprecated, this is the breaking commit:

This commit disables promise unwrapping and adds $parseProvider.unwrapPromises() getter/setter api that allows developers to turn the feature back on if needed. Promise unwrapping support will be removed from Angular in the future and this setting only allows for enabling it during transitional period.

..........

Previously promises found anywhere in the expression during expression evaluation would evaluate to undefined while unresolved and to the fulfillment value if fulfilled.

..........

BREAKING CHANGE: $parse and templates in general will no longer automatically unwrap promises. This feature has been deprecated and if absolutely needed, it can be reenabled during transitional period via $parseProvider.unwrapPromises(true) api.

You can still enable it with $parseProvider like so:

angular.module('Lunch.controllers', [])
  .config( ($parseProvider) ->
    $parseProvider.unwrapPromises(true)
    $parseProvider.logPromiseWarnings(false)
  )

But it would break in future versions (as mentioned above), so instead do this:

angular.module('Lunch.controllers', [])
  .controller( 'LunchCtrl', ($scope, LunchMates) ->
    LunchMates.getLunchMates().then (data)->
      $scope.lunchers = data
  )

This issue (among some others) is very common, mostly because lots of tutorials & books that new developers find all over the web, was written before version 1.2 and therefore not up-to-date. Always keep yourself up-to-date with the https://github.com/angular/angular.js/blob/master/CHANGELOG.md

这篇关于这是否承诺看起来是正确的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆