仅当ng-if为true时才调用方法 [英] calling a method only if ng-if is true

查看:149
本文介绍了仅当ng-if为true时才调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅当ng-if中的条件为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>

这是js代码

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

从控制台我可以看到,此方法比item.length和i.loc条件触发的次数更多.因此,只有在ng-if为true时,才如何在内部方法中调用此方法

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

推荐答案

如果条件为true,则将调用someMethod().例如,

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>

这篇关于仅当ng-if为true时才调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆