LIVE DEMO
考虑以下myButton指令:
angular.module("Demo",[]).directive("myButton",function() { return { restrict : "E",replace: true,scope: { disabled: "=" },transclude: true,template: "<div class='my-button' ng-class='{ \"my-button-disabled\": disabled }' ng-transclude></div>",}; });
可以像这样使用:
<my-button disabled="buttonIsDisabled" ng-click="showSomething = !showSomething"> Toggle Something </my-button>
当buttonIsDisabled为true时,如何停止执行ng-click?
解决方法
为什么不使用按钮的实际按钮.您可以将指令更改为:
angular.module("Demo",template: "<button class='my-button' ng-class='{ \"my-button-disabled\": disabled }' ng-disabled='disabled' type='button' ng-transclude></button>" }; });
然后将它设计为看起来像你的div.见我制作的Short Example.