角模板调用函数可以返回的承诺 [英] Can Angular template call function returning promise

查看:90
本文介绍了角模板调用函数可以返回的承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

角的$ Q文档说:$ Q承诺是由模板引擎认可在角,这意味着,在模板中,你可以把连接到一个范围,好像他们是所得值的承诺。

Angular's $q documentation says "$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."

角度的视图模板也让您评估前pressions,这意味着你可以调用从范围公开的函数。

Angular's view templates also let you evaluate expressions, which means you can call functions exposed from the scope.

我发现一个视图模板可以参考实际值,承诺(最终解决实际值),或退回实际值的功能,但并不功能返回的承诺,它总是呈现 {} 到视图模板。

I've found that a view template is able to refer to real values, promises (which eventually resolve to real values), or functions returning real values, but not functions returning promises, which always render {} into the view template.

我创建小提琴与此的一个例子。

任何人都可以在正确的方向做的更好或引导我?

Can anyone do better or steer me in the right direction?

(也许在视图模板使用的函数是一个坏主意;还有其他的问题,这种技术,因为角度看功能无法判断是否是没有调用该函数改变,所以观看功能得到每个评价消化周期。我已经看到了这些 2 questions 并在两人面前,推荐答案是使用promise.then改变对范围内正常的财产,并有看法模板观看普通财产而不是承诺。我得到的,但我用我想提供一些参数来计算的值,它的方便,直接在视图模板提供的参数。基本上我想要写的东西像url_for在或的 Rails的。)

(Perhaps using a function in the view template is a bad idea; there are other problems with this technique, because Angular watching a function can't tell whether it's changed without calling the function, so the watched function gets evaluated on every digest cycle. I've seen these two questions and in both of them, the recommended answer was to use promise.then to change a normal property on the scope, and have the view template watch the normal property instead of the promise. I get that, but for my use I want to supply some parameters to calculate a value, and it's convenient to supply those parameters directly in the view template. Basically I'm trying to write something like url_for in Flask or Rails.)

推荐答案

我不能完全确定,但如果你的承诺作为属性返回一个对象,它的工作原理。 (小提琴)

I'm not exactly sure, but if you return an object with the promise as a property, it works. (fiddle)

var deferred2 = $q.defer();
$scope.po = { promise: deferred2.promise };
$scope.getPo = function() {
    return $scope.po;
};

$timeout(function() {
    deferred2.resolve('some lazy text');
}, 2000);

HTML

<p>Function returning promise (object): {{ getPo().promise }}</p>

这篇关于角模板调用函数可以返回的承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆