简体   繁体   English

Angular2更改按钮禁用功能

[英]Angular2 change button disable state with a fuction

I have a simple form with a button that i want to control its disabled state using a simple function 我有一个带有按钮的简单表单,我想使用一个简单的函数来控制其禁用状态

so i wrote the following 所以我写了以下

<button pButton type="button" icon="fa-plus" iconPos="left" label="Add Device" class="ui-button-swat" (click) = "addDevice()" [disabled]="!checkAddDeviceConditions()"></button>

The function itsself is simple checking that some fields have values, but i noticed that the function is being called whenever the user moves the mouse, calling it many times in vain. 该函数本身很简单,可以检查某些字段是否具有值,但是我注意到,只要用户移动鼠标,就会多次调用该函数,但都是徒劳的。

im concerned about the issue, is it considered the correct way to control the disable property? 我担心这个问题,它是否被认为是控制disable属性的正确方法?

Correct or not is going to be fairly case specific. 正确与否取决于具体情况。 If you are noticing performance errors because of this, or the function is extremely intensive, then probably not. 如果您因此注意到性能错误,或者该功能过于密集,则可能不会。 If it is really simple, like it sounds it is in this case, then it probably doesn't matter much. 如果真的很简单(听起来像是在这种情况下),那么它可能并不重要。

However, if you want it to only run when those fields update, you could assign it to a variable inside of the checkAddDeviceConditions function, instead of returning a boolean, and put a 但是,如果您希望它仅在这些字段更新时才运行,则可以将其分配给checkAddDeviceConditions函数内部的变量,而不是返回布尔值,并放置一个

(change)="checkAddDeviceConditions()"

or 要么

(input)="checkAddDeviceConditions()"

on each field you want to trigger a checking of the update. 在每个要触发更新检查的字段上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM