Search code examples
htmlangulartypescriptangular-components

Can't bind to 'disabled' since it isn't a known property of 'app-button'


how can i bind disabled property to my button component ?

i was trying to add disabled to HTML file where it was supposed to take it as an input in button component (like color , font color are text are taken as inputs) .. but im not clear how to do that

button.component.ts


@Component({
  selector: 'app-button',
  templateUrl: './button.component.html',
  styleUrls: ['./button.component.css']
})
export class ButtonComponent implements OnInit {

  @Input() text! : string;
  @Input() color! : string;
  @Input() fontColor! : string;

  @Output() btnClick = new EventEmitter;

  constructor() { }

  ngOnInit(): void {
  }

  onClick(){
    this.btnClick.emit();
  }

}

button.component.html

<button [ngStyle]="{'background-color':color,'color':fontColor}" class="btn" (click)="onClick()">
  {{text}}
</button>

registeration.component.html

  <h1>Register Online</h1>

  <form [formGroup]="registerationForm" (ngSubmit)="onSubmit()">
    <div class="form-group">
      <label>Name</label>
      <input type="text" class="form-control"
      placeholder="Enter Name" id="name" formControlName="name">
      <span class="text-danger"
      *ngIf="registerationForm.controls['name'].dirty && registerationForm.hasError('required','name')">*Name is a required feild</span>
    </div>
    <div class="form-group">
      <label>Contact Number</label>
      <input type="number" class="form-control"
      placeholder="Enter Contact Number" id="number" formControlName="number">
      <span class="text-danger"
      *ngIf="registerationForm.controls['number'].dirty && registerationForm.hasError('required','number')">*Number is a required feild</span>
    </div>
    <div class="form-group">
      <label>E-mail</label>
      <input type="email" class="form-control"
      placeholder="Enter E-mail" id="email" formControlName="email" >
      <span class="text-danger"
      *ngIf="registerationForm.controls['email'].dirty && registerationForm.hasError('required','email')">*Email is a required feild</span>
    </div>
  </form>

  <div class="container-fluid">
    <app-button color="red" fontColor="white" text="Go Back" (btnClick)="hoverBack()" class="buttons"></app-button>
    <app-button color="green" fontColor="white" text="Register"
    [disabled]="" (btnClick)="register()" class="buttons"></app-button>
  </div>

</div>

error description image


Solution

  • like button is a custom component

    you will have to defined disable as input of your app-button to be able to pass it to the true button tag <button>

    @Input() disabled: boolean;
    

    full sample will give

    @Component({
      selector: 'app-button',
      templateUrl: './button.component.html',
      styleUrls: ['./button.component.css']
    })
    export class ButtonComponent implements OnInit {
    
      @Input() text! : string;
      @Input() color! : string;
      @Input() fontColor! : string;
      @Input() disabled! : boolean;
    
      @Output() btnClick = new EventEmitter;
    
      constructor() { }
    
      ngOnInit(): void {
      }
    
      onClick(){
        this.btnClick.emit();
      }
    

    button.component.html

    <button [ngStyle]="{'background-color':color,'color':fontColor}" class="btn" (click)="onClick()" [disabled]="disabled">
      {{text}}
    </button>