标签: javascript angularjs
如果我有一个返回对象的函数。例如
$scope.getPoint = function() { //some calculation logic goes here return {x:1,y:2}; }
我想在模板中显示它的属性:
<b>som html</b> x: {{getPoint().x}} y:{{getPoint().y}}
这将导致对函数的两次调用。 (我知道,无论如何,棱角可能会称它为无数次)
有没有办法在模板中重复使用相同的返回值?
答案 0 :(得分:1)
类似的东西:
<b>som html</b> <span ng-repeat="(key, value) in getPoint()">{{key}}: {{value}}</span>
应该有效,至少根据docs。
编辑:实际上它确实:Plunk
答案 1 :(得分:0)
只要价值不会改变,为什么不把它变成一个自我调用函数呢?
$scope.myVar = (function () { return { x: 1, y: 2 }; }());