当我在angularjs中按下一个按钮时如何禁用其他按钮 [英] How can I disable other buttons when I press one button in angularjs
问题描述
有可能做到这一点吗?当我按下一个按钮元素时,其他按钮元素将自动被禁用.我看到了一个类似的问题,但在纯粹的angularjs中寻找了这个问题.
Is it possible to do this in angular? When I press one single button element, other button elements automatically get disabled. I saw a similar question but looking for this in pure angularjs.
<html ng-app>
<button ng-model="button" > abc </button>
<button ng-disabled="button"> def </button>
<button ng-disabled="button"> ghi </button>
</html>
推荐答案
您可以将 ng-click
事件绑定到所有三个按钮,将标识符传递给函数(即 id
.然后,您的函数会将变量绑定到作用域,然后可以使用该变量来确定 ng-disabled
是否应处于活动状态.
You can bind an ng-click
event to all three of the buttons, passing an identifier to the function (i.e. the id
of the button. Your function would then bind a variable to the scope, which you could then use to determine if ng-disabled
should be active or not.
例如,在您的控制器中,您将具有类似于以下内容的信息:
For example, in your controller you would have something similar to:
$scope.selectedButton;
$scope.selectButton = function(id) {
$scope.selectedButton = id;
}
然后,在您的HTML中;您将对其进行修改,以考虑上述 ng-click
和 ng-disabled
的注意事项.
Then, in your HTML; you would revise it to take in the ng-click
and ng-disabled
considerations mentioned above.
<html ng-app ng-controller="SomeController">
<button ng-disabled="selectedButton && selectedButton != 'abc'" ng-click="selectButton('abc')">abc</button>
<button ng-disabled="selectedButton && selectedButton != 'def'" ng-click="selectButton('def')">def</button>
<button ng-disabled="selectedButton && selectedButton != 'ghi'" ng-click="selectButton('ghi')">ghi</button>
</html>
注意,检查selectedButton和selectedButton是否不等于 foo
的逻辑是确定已选择一个按钮,因此该变量设置为作用域.
Note, the logic of checking if selectedButton and the selectedButton does not equal foo
, is to determine that a button has been selected, thus the variable is set to scope.
这篇关于当我在angularjs中按下一个按钮时如何禁用其他按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!