Javascript 从 Angularjs 上的控制器启用/禁用按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30596488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enable/Disable button from controller on Angularjs
提问by Pez
I have an HTML button option as the following,
我有一个 HTML 按钮选项,如下所示,
<button ng-class="{'primaryButton':true}" type="button" id="modalCreateBtn" "ng-hide="false" class="btn btn-group mm-btn-save primaryButton" ng-click="addSegments()">CREATE</button>
There is no ng-disableoption in the above button option. Is this possible to enable/disable button with buttonId on controller? Also, I dont want to add disable option on HTML view. Instead I want to control it via scripts. Is this possible?
ng-disable上面的按钮选项中没有选项。这是否可以启用/禁用控制器上带有 buttonId 的按钮?另外,我不想在 HTML 视图上添加禁用选项。相反,我想通过脚本来控制它。这可能吗?
回答by Gabriel Kohen
Have you looked into ngDisable? You can have an ngModel and change it from the controller. Like the documentation example says here:
你有没有研究过ngDisable?您可以拥有一个 ngModel 并从控制器更改它。就像这里的文档示例所说:
<span ng-controller="MyController as myController">
<label>Click me to toggle: <input type="checkbox" ng-model="myController.checked"></label><br/>
<button ng-model="button" ng-disabled="myController.checked">Button</button>
</span>
And the JS:
和JS:
angular.module('controllerAsExample', [])
.controller('MyController ', function(){
this.checked = false;
// this.checked = true;
});
回答by Alhuck A
Using ng-disabledis the best practice to enable/disable a button, but you can also achieve it in the controller without adding disabledproperty to your HTML view with this scripts,
使用ng-disabled是启用/禁用按钮的最佳实践,但您也可以在控制器中实现它,而无需disabled使用此脚本向 HTML 视图添加属性,
angular.element(document.getElementById('yourButtonId'))[0].disabled = true;
angular.element(document.getElementById('yourButtonId'))[0].disabled = true;
In your case,
在你的情况下,
angular.element(document.getElementById('modalCreateBtn'))[0].disabled = true;
angular.element(document.getElementById('modalCreateBtn'))[0].disabled = true;
Hope this helps!
希望这可以帮助!
回答by tyne
Have you tried using ng-if ?
你试过使用 ng-if 吗?
Controller :
控制器 :
$scope.setUploadMode = function (stats) {
if (stats == 4) {
$scope.selectMe = false;
} else { alert("No"); $scope.selectMe = true; }
};
$scope.hello = function () {
alert("YEY");
};
HTML :
HTML :
div class="radio">
<label>
<input type="radio" name="appendCreateMode" ng-disabled="uploadMode != 2" ng-click="setUploadMode(4)" />Append Folder
</label>
</div>
<button ng-model="button" ng-disabled="selectMe" ng-click="hello()">Button</button

