如何在angularjs上禁用或启用按钮?

时间:2017-03-02 09:06:41

标签: angularjs angularjs-scope

我在表单中使用了2个按钮。我想在初始化时禁用button1虽然我已经给出ng-disabled="true"但是每当用户点击button2时,button1就会启用。谁能告诉我如何在angularjs中做到这一点?

4 个答案:

答案 0 :(得分:2)

如果您不使用控制器内的范围变量,则不需要在控制器中执行任何操作。

所以只需做一些事情:



angular.module("app",[]).controller("ctrl",function($scope){})

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
        <div ng-app="app" ng-controller="ctrl">
          <button ng-disabled="first !== true"> one</button>
          <button ng-click="first = true"> two</button>
        </div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

使用范围变量并将其分配给<a href="\\.....\....\...\..\2_7.jpg">file</a>

&#13;
&#13;
ng-disabled
&#13;
angular.module("app",[])
.controller("ctrl",function($scope){
	$scope.firstBtn = true;
	$scope.secondBtn = false;
})
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您可以点击第二个按钮调用一个功能,并将ng-disabled值设置为false

<强>样本

&#13;
&#13;
var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl',function($scope) {
  $scope.inactive= true;
  $scope.enableButton1 = function() {
    $scope.inactive= false; 
  }      
});
&#13;
<div ng-app="myApp" ng-controller="MyCtrl">
  <br><input type="button" ng-disabled="inactive" value="button1"/>
  <br><input type="button" ng-click="enableButton1()"  value="button2"/>
</div>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

如下所示非常简单:

<强> JS

var app = angular.module('myApp', []);
app.controller('ctrl', function($scope) {
  $scope.isDisabled = true;
  $scope.toggleButton = function() {
    $scope.isDisabled = !$scope.isDisabled;
  }
});

<强> HTML

<div ng-app='myApp'>
  <div ng-controller='ctrl'>
    <button ng-click='toggleButton()'>
      Toggle
    </button>
    <button ng-disabled='isDisabled'>
      I am button
    </button>
  </div>
</div>

这是jsfiddle链接Jsfiddle demo