gpt4 book ai didi

AngularJS promise

转载 作者:行者123 更新时间:2023-12-04 03:29:06 25 4
gpt4 key购买 nike

AngularJS docs say:

$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.



所以有人可以解释这个 fiddle不起作用的原因吗?不可能更改文本字段的值。但是分配保证$ http服务返回到作用域字段的 promise 就像是一种魅力。

Controller :
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">

最佳答案

您需要在promise对象上使用then()函数:

this.getItem().then(function(result) {
$scope.item = result;
});

就您而言,我认为您不需要 promise 。 Angular的$ watch系统将处理所有事情。 Just return an object in your function, not a primitive type:
this.getItem = function () {
var item = {};

// do some async stuff
$http.get(...).success(function(result) {
item.title = result;
});
return item;
};

$scope.item = this.getItem();

关于AngularJS promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14080095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com