在Angular 4中,可以使用@Input
装饰器来创建一个可禁用的按钮组件。以下是一个示例解决方案:
button.component.ts
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-button',
template: `
`
})
export class ButtonComponent {
@Input() label: string;
@Input() disabled: boolean;
}
ButtonComponent
并在模板中使用它:parent.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-parent',
template: `
`
})
export class ParentComponent { }
ButtonComponent
作为一个自定义的元素引入,并传递label
和disabled
属性。这样,按钮组件就会根据传入的disabled
属性值来禁用或启用按钮。
希望这个示例能对你有帮助!