要根据父类来禁用按钮,您可以使用Angular的属性绑定和条件语句来实现。以下是一个示例代码:
在父组件中,定义一个布尔类型的变量来表示按钮是否应该被禁用。例如:
parent.component.ts:
export class ParentComponent {
disableButton: boolean = true;
}
在父组件的模板中,将该变量与子组件中的按钮绑定,并传递给子组件:
parent.component.html:
在子组件中,接收父组件传递过来的disableButton变量,并将其用于按钮的disabled属性:
child.component.ts:
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-child',
template: `
`
})
export class ChildComponent {
@Input() disableButton: boolean;
}
这样,当父组件中的disableButton值为true时,按钮会被禁用。反之,当disableButton值为false时,按钮会可用。
希望这个示例能帮到您!