Search code examples
javascripthtmlangularjsangularangular2-databinding

without using form for validation How can I disabled a button in angular if <input> field is empty


I am trying to do @output in angular for that I have an input field with a button, I will send input's field data to parent by using @output() I want to disabled a button if the input field is empty, How can I disable without using form validation.

I tried this code

  <input type="text" [(ngModel)] = "childInput" >
  <button [disabled] ="!childInput.value" (click)="sendToParent()">Send To Parent</button>

but it keeps disabled the button if the input field is empty or not.

P.S I found answer with form validation but I didn't want to do with form validation or defining a bool variable


Solution

  • this code will work

    <input type="text" [(ngModel)] = "childInput" >
    <button [disabled] ="!childInput" (click)="sendToParent()">Send To Parent</button>