在 AngularJS 中,有什么办法可以制作一个ng-click
依赖于布尔值?
例如,我希望以下文本(“Click”)可点击,但是only当某些范围属性(例如,$rootScope.enableClick
) is true
.
<div ng-click="count = count + 1" ng-init="count=0">Click</div>
最好,是否有一种直接的方法可以在不创建自定义指令的情况下执行此操作?
用奇异而美妙的方式 in the ng-click
表达:
<div ng-click="!stop && (count = count + 1)">Click me</div>
The DEMO
.clickable {
cursor: pointer;
}
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app ng-init="count=0">
<div ng-click="!stop && (count = count + 1)" class="clickable" >Click Me</div>
<div>Count={{count}}</div>
<input type="checkbox" ng-model="stop">Stop counting<br>
</body>
Update
Or use a button with the ng-disabled https://docs.angularjs.org/api/ng/directive/ngDisabled directive:
<button ng-click="count = count + 1" ng-disabled="stop" >Click Me</button>
The NG-禁用 https://docs.angularjs.org/api/ng/directive/ngDisabled指令不适用于<div>
元素。这disabled
财产不是全局属性 https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes. The ng-disabled
指令仅适用于<input>
, <button>
, <textarea>
, and <select>
元素,因为这些元素支持disabled财产 https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/disabled.
The DEMO
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app ng-init="count=0">
<button ng-click="count = count + 1" ng-disabled="stop" >Click Me</button>
<div>Count={{count}}</div>
<input type="checkbox" ng-model="stop">Stop counting<br>
</body>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)