FarazShuja
FarazShuja

Reputation: 2370

calling a method only if ng-if is true

Is it possible to call a method only when a condition in ng-if is true? I have a repeat like this

<div ng-repeat="i in items" ng-if="i.loc == 'abc'">
    <h1>Hello.. {{getName()}}</h1>
</div>

here is the js code

$scope.getName = function() {
    console.log('fired');
    return "myName";
}

From console I can see that this method is firing many more times then items.length and i.loc condition. So how to call this inside method only when ng-if is true

Upvotes: 22

Views: 51970

Answers (2)

Rizwan
Rizwan

Reputation: 4433

if you want to call function when condition is true or false, you can try this:

<md-card
        class="ew-service-card"
        ng-click="campaign.status == 'completed' ? $ctrl.newServiceCard() : $ctrl.newServiceCardDisable()">
 </md-card>

Upvotes: 0

Dinesh ML
Dinesh ML

Reputation: 931

If the condition is true , someMethod() will be called. For example,

<div ng-repeat="i in items" 
     ng-if="i.name == 'abc'" 
     ng-init="someMethod()">
</div>

Upvotes: 51

Related Questions