我是angularJs的新手,所以请袒露愚蠢的问题。我正在尝试为click事件上的按钮实现动态行为,但它的工作是例外,是因为某些范围吗?
这是我的Html代码:
<html ng-app="loopModule">
<head>
<title>Looping Example</title>
<script type="text/javascript" src="js/lib/angular.js"></script>
<script type="text/javascript" src="js/lib/angular-sanitize.js"></script>
<script type="text/javascript" src="js/controllers/LoopController.js"></script>
</head>
<body>
<div ng-controller="LoopController">
<strong>Using NG-DISABLED :</strong><br/>
<button ng-disabled="isButtonDisabled" >I am Button</button>
<button ng-click="disableButton(isButtonDisabled)" >Click Me</button>
<br/>
</div>
</body>
</html>
这是控制器:
var eventDrivenSample = angular.module('eventDrivenPackage', []);
loopModule.controller('LoopController' ,
function loopController($scope ,$window){
$scope.isButtonDisabled = true;
$scope.disableButton =function(isButtonDisabled){
if(isButtonDisabled)
isButtonDisabled = false;
else
isButtonDisabled = true;
return isButtonDisabled;
}
}
);
当我点击“点击我”按钮但是如果我更换时,问题是按钮未启用 点击我 有了这个 点击我
然后按钮被启用为例外。我究竟做错了什么?。任何身体请帮助..
答案 0 :(得分:1)
将您的控制器更改为:
var eventDrivenSample = angular.module('eventDrivenPackage', []);
loopModule.controller('LoopController' ,
function loopController($scope ,$window){
$scope.isButtonDisabled = true;
$scope.disableButton = function(isButtonDisabled){
$scope.isButtonDisabled = !isButtonDisabled;
}
}
);
您的错误在于您没有设置范围属性的值(即$scope.isButtonDisabled
),而是设置方法disableButton
参数,而AngularJS并未对其进行监视。