【问题标题】:How can I disable other buttons when I press one button in angularjs当我在angularjs中按下一个按钮时如何禁用其他按钮
【发布时间】:2016-01-06 05:47:53
【问题描述】:

是否有可能以角度来做到这一点?当我按下一个按钮元素时,其他按钮元素会自动被禁用。我看到了一个类似的问题,但在纯 angularjs 中寻找这个问题。

<html ng-app>
  <button ng-model="button" > abc  </button> 
  <button ng-disabled="button"> def </button> 
  <button ng-disabled="button"> ghi </button>
</html>

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    您可以将ng-click 事件绑定到所有三个按钮,将标识符传递给函数(即按钮的id。然后您的函数会将变量绑定到范围,然后您可以使用它确定ng-disabled 是否应该处于活动状态。

    例如,在您的控制器中,您将有类似的内容:

        $scope.selectedButton;
    
        $scope.selectButton = function(id) {
            $scope.selectedButton = id;
        }
    

    然后,在您的 HTML 中;您将对其进行修改以考虑上述ng-click 和ng-disabled 考虑因素。

    <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 的逻辑是确定一个按钮已被选中,因此将变量设置为范围。

    【讨论】:

    • 谢谢大家。我喜欢这个答案。
    【解决方案2】:

    简单,纯粹的角度方式。

    <html ng-app>
      <button ng-click="buttonsDisabled = true"> abc  </button> 
      <button ng-disabled="buttonsDisabled"> def </button> 
      <button ng-disabled="buttonsDisabled"> ghi </button>
    </html>
    

    http://jsfiddle.net/HB7LU/21811/

    【讨论】:

    • 作者并没有说它应该适用于每个按钮。看看他的例子。 “单键”和“任意键”是有区别的。
    【解决方案3】:
    <html ng-app="yourApp">
      <body ng-controller="buttonCtrl">
        <button ng-disabled="!button1" ng-click="toggleFunction(1)" > abc  </button> 
        <button ng-disabled="!button2" ng-click="toggleFunction(2)" > abc  </button> 
        <button ng-disabled="!button3" ng-click="toggleFunction(3)" > abc  </button> 
      </body>
    </html>
    
    <script>
    
    yourApp.controller('buttonCtrl', ['$scope', function($scope) {
      function toggleFunction(activeBtn){
          if (activeBtn == 1){
              $scope.button1 = true;
              $scope.button2 = false;
              $scope.button3 = false;
          } else if (activeBtn == 2){
              $scope.button1 = false;
              $scope.button2 = true;
              $scope.button3 = false;
          } else if (activeBtn == 3){
              $scope.button1 = false;
              $scope.button2 = false;
              $scope.button3 = true;
          }
      }
    }]);
    </script>
    

    【讨论】:

      【解决方案4】:

      你可以这样做

      <script>
      var app=angular.module('myApp', []);
      app.controller('someCtrl',function($scope){
        $scope.mySwitch=true;
        $scope.toggle=function(){
          $scope.mySwitch=!$scope.mySwitch;
        };
      });
      </script>
      <div ng-app="myApp">
        <div ng-controller="someCtrl">
          <p>
            <button ng-click="toggle()">abc</button>
          </p>
          <p>
            <button ng-disabled="mySwitch">def</button>
            <button ng-disabled="mySwitch">ghi</button>
          </p>
        </div> 
      </div>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-02
        • 2019-02-24
        • 2011-07-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多