【发布时间】:2016-01-21 02:52:28
【问题描述】:
我正在研究以下指令:
module.directive('myButton', function () {
var directive = {
compile: compile,
restrict: 'E',
scope: {
type: '@',
click: '&',
disabled: '@'
},
template: '<a class="my-button my-button-{{type}}" data-ng-click="disabled || click()"><ng-transclude /></a>',
transclude: true
};
function compile(element, attributes) {
if (typeof attributes.click == 'undefined') attributes.click = function () { };
if (typeof attributes.disabled== 'undefined') attributes.disabled = false;
}
return directive;
});
我在一个看起来像这样的页面上有一些假人:
<my-button type="1a" click="alert('Button 1a works!')" disabled="false">Test A</my-button>
我似乎无法使按钮的单击和禁用功能正常工作(至少不能同时工作)。我已经设置了锚标记的样式,使它看起来像一个按钮。锚标记不受disabled 属性的影响,因此ngClick 必须检查它是否应该触发。
如何让点击和禁用功能按预期工作?
【问题讨论】:
标签: angularjs angularjs-directive