2017-05-24 55 views
0

如果ng-if =“item.value”返回true,我想从中调用控制器中存在的函数。下面是摘录当ng-if =“variable”为真时寻找调用控制器函数

<td class="rsp-p1"> 
     <span ng-if="item.result"> 
      {$ ctrl.hw_interface $}</span> 
</td> 

所以在这里,我要寻找的流量如果异常,如果返回true调用一个函数driverInfo(),然后将该功能设置hw_interface变量里面我想要的价值,因此我显示该以上代码片段的值为$ strl.hw_interface 但我无法找到适当的方法来做到这一点。我尝试了ng-init()。您的输入非常赞赏:)

这里是我的angularjs代码

function driverInfo() { 
    ironic.driverInfo(ctrl.driver,'power').then(function(response){ 
    alert(response) 
    ctrl.hw_interface = response; 
    }); 
} 

(此功能的签名,现在没有一个参数,一旦我搞清楚打电话给我将更新签名的方式。)

+0

您可以发布您AngularJS代码? – chakeda

+0

当然更新了描述 – user3339691

回答

1

ng-init应该有工作就好了:

<span ng-if="item.result" ng-init="yourFn()"></span> 

但是,你知道什么会导致item.result改变?你可以对控制器中的变化做出反应,并在那里调用你的函数吗?

+0

恩,让我看看。其实我可以,因为“项目”,我正在阅读从不同的控制器方法。 – user3339691

+0

其实你的想法引发我检查对item.result作出反应的方法,基本上这是一个init(),因此可以从它本身调用driverInfo()。解决问题。非常感谢 :) – user3339691

1

也许你可以尝试一些像

ctrl.myfunc = function() { 
    //do some time consuming work 
    return false; //true; 
}; 

<td class="rsp-p1"> 
    <span ng-if="ctrl.myfunct()"> 
     {{ ctrl.hw_interface }} 
    </span> 
</td> 
+0

嗨DelhiPanda,感谢您的输入,但我想检查一些其他变量,如前所述的item.result,如果返回true调用一个函数并显示hw_interface。以上建议的答案不会检查item.result – user3339691

+0

在该函数中可以检查任何具有任何参数的项目并返回false或true,或者在** ng-if **中可以用'ng-if =“item.result === 1 && ctrl.hw_interface === true“'或者如果你只需要这个定义'ng-if =”item.result && ctrl.hw_interface“' – DelhiPanda

1

你试图把你的driverInfo()函数的跨度,而不是里面? driverInfo函数返回你想要hw_interface变量的值是什么?因此,如果item.result返回一个真值,则driverInfo函数将运行返回跨度中hw-interface的值。

<td class="rsp-p1"> 
 
     <span ng-if="item.result"> 
 
      {$ctrl.driverInfo() }</span> 
 
</td>

+0

感谢您的输入,我确实尝试了上面的像{$ ctrl.driverInfo()$},但没有奏效。你认为最后的“$”会造成一些问题吗? – user3339691

+0

是的,后面的$不会允许函数正常运行,实际上现在我再次查看它,你需要2个大括号。 – PandaScript

+0

好吧,让我试试这个,然后,:) – user3339691

0

基于DelhiPanda的回答是:

ctrl.myfunc = function() { 
    //do some time consuming work 
    return false; //true; 
}; 

<td class="rsp-p1"> 
    <span ng-if="item.result && ctrl.myfunct()"> 
     {{ ctrl.hw_interface $ }} 
    </span> 
</td> 

推理:在检查条件语句,如果两个参数必须truthy,第二个参数将不若第一价值论点是错误的。换句话说,该功能将不会运行。

希望这会有所帮助!

0

将driverInfo()函数放在div中,使用条件块就可以工作。

<div ng-if="item.result"> 
 

 
    {$ctrl.driverInfo() } 
 

 
</div>

相关问题