问题描述
升级到 1.2 后,我的服务返回的承诺表现不同...简单服务 myDates:
After upgrading to 1.2, promises returned by my services behave differently...Simple service myDates:
getDates: function () {
var deferred = $q.defer();
$http.get(aGoodURL).
success(function (data, status, headers, config) {
deferred.resolve(data); // we get to here fine.
})......
在早期版本中,我可以在我的控制器中执行:
In earlier version I could just do, in my controller:
$scope.theDates = myDates.getDates();
并且从 getDates 返回的 promise 可以直接绑定到 Select 元素.现在这不起作用,我被迫在我的控制器中提供对承诺的回调,否则数据将无法绑定:
and the promises returned from getDates could be bound directly to a Select element.Now this doesn't work and I'm forced to supply a callback on the promise in my controller or the data wont bind:
$scope.theDates = matchDates.getDates();
$scope.theDates.then(function (data) {
$scope.theDates = data; // this wasn't necessary in the past
文档仍然说:
$q 承诺被模板引擎以 angular 识别,这意味着在模板中,您可以将附加到范围的承诺视为结果值.
$q promises are recognized by the templating engine in angular, which means that in templates you can treat promises attached to a scope as if they were the resulting values.
他们(承诺)在旧版本的 Angular 中工作,但在 1.2 RC3 中自动绑定在我所有的简单服务中都失败了......关于我可能做错了什么的任何想法.
They (promises) were working in older versions of Angular but in the 1.2 RC3 automatic binding fails in all my simple services.... any ideas on what I might be doing wrong.
推荐答案
1.2.0-rc3有改动,包括你提到的:
There are changes in 1.2.0-rc3, including one you mentioned:
AngularJS 1.2.0-rc3 ferocious-twitch 修复了一些高优先级的问题$compile 和 $animate 中的问题并为 1.2 铺平了道路.此版本还引入了一些重要的突破性更改,这些更改在某些情况下可能会破坏您的指令和模板.请请务必阅读更改日志以了解这些更改并学习如果需要,如何迁移您的代码.有关此版本的完整详细信息,请参阅更改日志.
更改日志中有说明:
$parse:
- 由于5dc35b52,$parse 和模板一般将不再自动展开承诺.此功能已被弃用,并且如果绝对需要,可以在过渡期间重新启用通过
$parseProvider.unwrapPromises(true)
api. - 由于 b6a37d11,在 rc.2 中添加的功能可以解开函数的返回值是承诺(如果承诺解包是启用 - 见上一点),由于打破流行使用模式.
- due to 5dc35b52, $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. - due to b6a37d11, feature added in rc.2 that unwraps return values from functions if the values are promises (if promise unwrapping is enabled - see previous point), was reverted due to breaking a popular usage pattern.
这篇关于Angularjs 承诺不绑定到 1.2 中的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!