javascript - Angularjs 1.5x: How to call a function only once in a template and reuse the result in multiple places -
i have count of questions left in each section , count displayed user unless there 0 left in case display check mark.
<p class="list-group-item-heading">{{section.title}} <i ng-show="questionsleft(section)==0" class="fa fa-check"></i> <span ng-show="questionsleft(section)>0" class="label"> {{questionsleft(section)}}</span> </p>
the function questionsleft() called 3 times - how can called once , result reused? have tried both ng-init
, {{x = questionsleft(section); ""}} trick both of initialize variable not update when value changes.
- create variable inside
section
component controller. - put variable instead of function inside template.
- update variable value each time number of questions changed
questionsleft
function. can inside $onchanges hook, instance.
p.s. easier if provide controller code
Comments
Post a Comment