原来我喜欢的那种功能,叫做promise的autounrapping。但是1.2开始,这个功能被deprecated。伤心。
上代码:
angular.
module('myApp', ['ngResource'])
.config(function($parseProvider){
$parseProvider.unwrapPromises(true);
})
.controller('MyController', function($scope, Object) {
$scope.objects = Object.query(['A', 'B', 'C']);
})
.service('Object', function($q, $timeout) {
this.query = function(objects) {
var deferred = $q.defer();
$timeout(function() {
deferred.notify('notified');
console.log('promise notified');
if (new Date().getTime() % 2) {
deferred.resolve(objects);
} else {
deferred.reject(['D']);
}
}, 1000);
return deferred.promise;
};
});
参考:
- Why I never really loved Angular auto-unwrapping promises in view
- Why no promise AngularJS 1.2.0+
- StackOverflow: Angularjs promise not binding to template in 1.2
- Github Issue
发表回复