这个承诺看起来正确吗?

编程入门 行业动态 更新时间:2024-10-24 07:31:06
本文介绍了这个承诺看起来正确吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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 版之前编写的,因此不是最新的.始终使用 github 让自己保持最新状态/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 github/angular/angular.js/blob/master/CHANGELOG.md

更多推荐

这个承诺看起来正确吗?

本文发布于:2023-11-17 03:00:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608565.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正确

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!