这个承诺看起来正确吗? [英] does this promise look correct?

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

问题描述

这似乎应该将数据传递到我的范围,但事实并非如此,下面的代码是否有任何直接跳出错误的地方?

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()
  )

推荐答案

这段代码:$scope.lunchers = LunchMates.getLunchMates() 在范围上设置了一个承诺,它依赖于一个旧的已弃用功能.

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

版本 >=1.2 开始,promise unwrapping 已弃用,这是 中断提交:

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

此提交禁用承诺展开并添加$parseProvider.unwrapPromises() getter/setter api,允许开发人员如果需要,重新打开该功能.承诺展开支持将将来会从 Angular 中删除,并且此设置仅允许在过渡期间启用它.

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.

.......

以前在表达式中的任何地方都可以找到的 promise评估将在未解决时评估为 undefined 和履行时的履行价值.

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

.......

重大变化:$parse 和一般模板将不再自动解包承诺.此功能已被弃用,并且如果绝对需要,它可以在过渡期间重新启用通过 $parseProvider.unwrapPromises(true) api.

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.

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

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
  )

这个问题(以及其他一些问题)很常见,主要是因为很多教程和新开发人员在网上找到的书籍是在 1.2 版之前编写的,因此不是最新的.始终使用 https://github.com 让自己保持最新状态/angular/angular.js/blob/master/CHANGELOG.md

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天全站免登陆