【发布时间】:2018-11-24 09:31:17
【问题描述】:
我有一个通用组件app-button,其中包含一个按钮元素。
按钮组件html
<button [type]="type" [disabled]="isLoading || !!disabled">
<ng-content></ng-content>
</button>
按钮组件ts
@Component({
selector: 'app-button',
templateUrl: 'views/button.component.html',
styleUrls: ['styles/button.component.scss']
})
export class ButtonComponent implements OnInit {
@Input() type: string;
@Input() color: string;
@Input() disabled: boolean;
@Input() class: string;
isLoading: boolean;
constructor() {
this.type = "submit";
}
}
我添加(单击)到我的组件,它工作正常。但是当我设置disabled 属性时,它会禁用按钮,但是当您单击内容时(单击)仍然有效。
<app-button (click)="signIn()" [disabled]="true">
<span>Signin</span>
</app-button>
【问题讨论】:
-
你不能禁用一个组件
-
在您的
signIn函数中,您检查事件目标的属性是否已禁用...或者如果您知道单击事件处理程序将被禁用,则不要附加点击事件处理程序 -
如果你想坚持这个逻辑,我想你可以绑定点击
<button>标签本身并将函数作为组件的属性传递。 -
但是有没有办法让它对所有按钮都通用?我已经有很多用途了:)
-
@Gbac 你的意思是像这样的[click]="func",然后在组件@Input() func: any; if(!disabled) { func(); }
标签: angular button components disabled-input