我有一个通用组件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>
最佳答案
我认为你的问题在于你 禁用您的子组件,并且您在父组件中拥有click事件
解决方案可能是将您的 (click)="signIn()"
移动到您的子组件,它将被禁用并添加一个 @Output
装饰器来接收来自子进程的回调
子组件 html
<button [type]="type" [disabled]="isLoading || !!disabled" (click)="signIn()">
<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;
@Output() callback = new EventEmitter();
isLoading: boolean;
constructor() {
this.type = "submit";
}
signIn(){
this.callback.emit();
}
}
父组件 html
<app-button (callback)="signIn()" [disabled]="true">
<span>Signin</span>
</app-button>
关于angular - 如何防止通过 Angular 父组件点击禁用按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50860288/