Home > Software design >  as per selection values in dropdown textbox will be enabled or disabled in angular
as per selection values in dropdown textbox will be enabled or disabled in angular

Time:06-17

I write the code according to me for one dropdown consisting of 2 values i.e. Enabled, Disabled. As per the selection, the textbox should be enabled and disabled. can anybody tell me how to

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-dropdown',
  templateUrl: './dropdown.component.html',
  styleUrls: ['./dropdown.component.css']
})
export class DropdownComponent implements OnInit {
  des = false

  constructor() { }
  
  onchange(){
  if (true) {
    this.des=false
    
  }
  else{
    this.des= true
  }
  }
  ngOnInit(): void {
  }

}
<select name="dropdown" id="this">select the correct value 
    (change)="onchange()"
    <option value="yes">yes</option>
    <option value="No">No</option>
</select>

<input type="text" [disabled]="!des">

take the value of options and what condition I can give in the if-else section

CodePudding user response:

In HTML file add event as a parameter in your case:

<select name="dropdown" (change)="onChange($event)">
  select the correct value
  <option value="No">No</option>
  <option value="yes">yes</option>
</select>

<input type="text" [disabled]="des">

and in ts file add function

  onChange(ev) {
    this.des = ev.target.value === 'yes'
  }
  • Related