AngularJS文档say:AngularJS承诺
$ Q承诺通过在角模板引擎,这意味着在模板中,你可以把连接到一个范围的承诺,如果他们得到的价值确认。
那么有人可以解释一下这个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
@Dogbert,这里是伪代码来说明我在说什么: '$ scope.item = $ http({method:'post',url:'/ find/my/item /'}) .then函数(响应){ return response.item; });' 另一个使用$ resource方法的例子可以在这个[tutorial](http://docs.angularjs.org/tutorial/step_11)中找到。从行开始: '请注意,PhoneListCtrl是如何替换的...用$ scope.phones = Phone.query();' –
糟糕,刚创建[测试示例](http://plnkr.co/edit/) VP1Td3WtdM0E7n5HJH3W?p = preview),并且它看起来没有与任何承诺一起工作 –