HKI345
HKI345

Reputation: 313

Invoke a function once if using along with ng-repeat using angular js

I have a function to be invoked only once when it is being used along with ng-if and ng-repeat.

 <td ng-if="!vm.monthView && vm.yearView=='contract-year'" nginit="vm.ContractYearBaselines()" class="baseline-data-field"  ng-repeat="baselineDatum in baseline.data track by $index"restrict-to="[0-9]">
                {{baselineDatum}}</td>

The function vm.ContractYearBaselines() is being invoked as many times the ng-repeat executes......thereby the function having the API calls as many times.....can we restrict the function to be executed only once along with ng-repeat?

Upvotes: 0

Views: 51

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222682

You can set up a boolean variable inside the function and based on it you can execute it,

vm.executed = false;
vm.ContractYearBaselines = function(){
   if(!vm.executed)
   {
      your conditions;
     vm.executed = true;
   }  
}

Upvotes: 1

Related Questions