【发布时间】:2012-12-14 08:14:52
【问题描述】:
AngularJS 文档say:
$q Promise 被模板引擎识别为 Angular,这意味着在模板中,您可以将附加到范围的 Promise 视为结果值。
那么有人可以解释一下fiddle 不起作用的原因吗?无法更改文本字段值。但是,将 $http 服务返回到范围字段的承诺分配起来就像一个魅力。
控制器:
function MyController($scope, $q, $timeout) {
this.getItem = function () {
var deferred = $q.defer();
deferred.resolve({
title: 'Some title'
});
return deferred.promise;
};
$scope.item = this.getItem();
}
HTML:
<input type="text" ng-model="item.title">
【问题讨论】:
-
你能告诉我你是如何分配一个由 $http 返回的承诺,它按照你想要的方式工作吗?
-
@Dogbert,这里是用来说明我在说什么的伪代码:
$scope.item = $http({method: 'post', url: '/find/my/item/'}) .then(function (response) { return response.item; });另一个使用 $resource 方法的例子可以在这个tutorial 中找到。从线路开始:Notice how in PhoneListCtrl we replaced ... with $scope.phones = Phone.query(); -
糟糕,刚刚创建了test example,它似乎不适用于任何承诺
-
大家好,要让它工作,我必须使用
.success(function(){}).then(function(r){if (r.data["my-result"])return r.data["my-result"];});