简体   繁体   English

AngularJS-禁用按钮单击复选框

[英]AngularJS - Disabling checkbox on button click

I have the following scenario: I have a set of checkboxes which are disabled by default. 我有以下情形:我有一组默认情况下禁用复选框。 Now, on clicking button 1, I need the checkboxes to be enabled and also show buttons 2 & 3 and hide button 1 at that point. 现在,在单击按钮1时,我需要启用复选框,并同时显示按钮2和3并隐藏按钮1。

On clicking on buttons 2 or 3, I need to disable the checkboxes again and show button 1 while hiding buttons 2 & 3. 在单击按钮2或3时,我需要再次禁用复选框,并在隐藏按钮2和3的同时显示按钮1。

I am new to AngularJS, and although I have tried with ng-Click and ng-Disabled, I have not managed to achieve what I need. 我是新来AngularJS,虽然我曾尝试与NG-Click和NG-残疾人,我还没有成功地实现我需要什么。 Here is a sample of what I have so far: 这是我到目前为止的样本:

 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <button ng-model="!clicked" ng-show="!clicked">Button 1</button> <button ng-model="clicked" ng-show="clicked">Button 2</button> <button ng-model="clicked" ng-show="clicked">Button 3</button> <div> <input type="checkbox" ng-disabled="clicked">Checkbox 1 <input type="checkbox" ng-disabled="clicked">Checkbox 2 <input type="checkbox" ng-disabled="clicked">Checkbox 3 <input type="checkbox" ng-disabled="clicked">Checkbox 4 <input type="checkbox" ng-disabled="clicked">Checkbox 5 </div> 

Can someone point me to the right direction? 有人可以指出我正确的方向吗? Am I missing something? 我想念什么吗?

Please check working example - http://plnkr.co/edit/niAmt0kfJyfAOYeEbVqL?p=preview 请检查工作示例-http://plnkr.co/edit/niAmt0kfJyfAOYeEbVqL?p=preview

HTML 的HTML

<button ng-show="clicked" ng-click="clicked = !clicked">Button 1</button>
<button ng-show="!clicked" ng-click="clicked = !clicked">Button 2</button>
<button ng-show="!clicked" ng-click="clicked = !clicked">Button 3</button>


<div>
  <input type="checkbox" ng-disabled="clicked">Checkbox 1
  <input type="checkbox" ng-disabled="clicked">Checkbox 2
  <input type="checkbox" ng-disabled="clicked">Checkbox 3
  <input type="checkbox" ng-disabled="clicked">Checkbox 4
  <input type="checkbox" ng-disabled="clicked">Checkbox 5

</div>

Controller 控制者

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
    $scope.clicked = true;
});

Write a controller to handle the buttons and provide the disabled condition for the checkboxes. 编写一个controller来处理按钮并为复选框提供禁用条件。 Controllers should handle the view logic, not the view itself. 控制器应该处理视图逻辑,而不是视图本身。

Controller (mycontroller.js): 控制器 (mycontroller.js):

angular.module("myApp")
.controller("MyController", function() {
    var self = this;

    self.checkboxesEnabled = false;

    self.enableBoxes = function() {
        self.checkboxesEnabled = true;
    }

    self.disableBoxes = function() {
        self.checkboxesEnabled = false;
    }
});

Page : 页面

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="mycontroller.js"></script>

<div ng-app="myApp" ng-controller="MyController as my">
    <button ng-show="!my.checkboxesEnabled" ng-click="my.enableBoxes">Button 1</button>
    <button ng-show="my.checkboxesEnabled" ng-click="my.disableBoxes">Button 2</button>
    <button ng-show="my.checkboxesEnabled" ng-click="my.disableBoxes">Button 3</button>

    <div>
      <input type="checkbox" ng-disabled="!my.checkboxesEnabled">Checkbox 1
      <input type="checkbox" ng-disabled="!my.checkboxesEnabled">Checkbox 2
      <input type="checkbox" ng-disabled="!my.checkboxesEnabled">Checkbox 3
      <input type="checkbox" ng-disabled="!my.checkboxesEnabled">Checkbox 4
      <input type="checkbox" ng-disabled="!my.checkboxesEnabled">Checkbox 5
    </div>
</div>

page; 页;

<div ng-controller="MyCtrl">
<input    type="checkbox"   ng-disabled="checkMe"    />Toggles
<button ng-click="check()">disable/enable</button></div>

controller 控制者

function MyCtrl($scope) {
    $scope.checkMe=false;
    $scope.check = function() {
    $scope.checkMe=!$scope.checkMe;
    }
}

You seem to misunderstand the usage of ng-model. 您似乎误解了ng-model的用法。 Check out the Angular docs for all the directives, they are quite straight-forward. 查看Angular文档中的所有指令,它们非常简单。

Find a working example of what you want here : http://jsfiddle.net/joshdmiller/hb7lu/ 在这里找到您想要的工作示例: http : //jsfiddle.net/joshdmiller/hb7lu/

<button ng-show="checkboxesDisabled" ng-click="checkboxesDisabled = !checkboxesDisabled">Button 1</button>
<button ng-hide="checkboxesDisabled" ng-click="checkboxesDisabled = !checkboxesDisabled">Button 2</button>
<button ng-hide="checkboxesDisabled" ng-click="checkboxesDisabled = !checkboxesDisabled">Button 3</button>


<div>
    <input type="checkbox" ng-disabled="checkboxesDisabled">Checkbox 1
    <input type="checkbox" ng-disabled="checkboxesDisabled">Checkbox 2
    <input type="checkbox" ng-disabled="checkboxesDisabled">Checkbox 3
    <input type="checkbox" ng-disabled="checkboxesDisabled">Checkbox 4
    <input type="checkbox" ng-disabled="checkboxesDisabled">Checkbox 5

</div>

Here is a working plunker for your scenario. 是适合您情况的工作插件。 You do not need to use ng-model on button. 您无需在按钮上使用ng-model。 It won't do anything there. 它在那里什么也不会做。 Plus, IMO checkBoxDisabled as a name makes more sense for the model. 另外,IMO checkBoxDisabled作为名称对模型更有意义。 There are a couple of more changes that I have done to make it work. 为了使它起作用,我还做了一些其他更改。

  <button ng-click="vm.toggleDisable()" ng-show="vm.checkBoxDisabled">Button 1</button>
  <button ng-click="vm.toggleDisable()" ng-show="!vm.checkBoxDisabled">Button 2</button>
  <button ng-click="vm.toggleDisable()" ng-show="!vm.checkBoxDisabled">Button 3</button>


  <div>
    <input type="checkbox" ng-disabled="vm.checkBoxDisabled">Checkbox 1
    <input type="checkbox" ng-disabled="vm.checkBoxDisabled">Checkbox 2
    <input type="checkbox" ng-disabled="vm.checkBoxDisabled">Checkbox 3
    <input type="checkbox" ng-disabled="vm.checkBoxDisabled">Checkbox 4
    <input type="checkbox" ng-disabled="vm.checkBoxDisabled">Checkbox 5

  </div>

Controller:- 控制器:

angular.module("myApp", [])
.controller('MyController', function() {
  var vm = this;
  vm.checkBoxDisabled = false;
  vm.toggleDisable = function() {
    vm.checkBoxDisabled = !vm.checkBoxDisabled;
  }
})

EDIT: As LionC suggested, you should ideally put your logic in controller. 编辑:正如LionC建议的那样,您最好将逻辑放在控制器中。 I have updated my answer and plunker accordingly. 我已经相应地更新了我的答案和朋克。

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

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