3

If I have a function that returns an object. eg.

$scope.getPoint = function()
{
    //some calculation logic goes here
    return {x:1,y:2};
}

And I want to display properties from it in a template:

<b>som html</b> x: {{getPoint().x}} y:{{getPoint().y}}

That would result in two calls to the function. (I know, angular may call it a gazillion times anyway)

Is there any way to reuse the same return value in a template?

4

2 回答 2

1

Something like:

<b>som html</b> <span ng-repeat="(key, value) in getPoint()">{{key}}: {{value}}</span>

should work, at least according to the docs.

EDIT: In fact it does: Plunk

于 2013-07-18T21:17:37.693 回答
0

As long as the value isn't going to change, why not make it a self-calling function?

$scope.myVar = (function () {
    return { x: 1, y: 2 };
}());
于 2013-07-18T21:09:41.600 回答