0

The set method can be operating element when the element is show(*ngIf = true).

But, when the input element is hide before, I went to get the input element value, the value will be return back server.

For example:

html, set #namePlaceholder input element:

    <input *ngIf="editNameActive" type="text" value="{{value}}" #namePlaceholder>
    <div (click)="editName()"></div>

component:

editNameActive control show or hide input element.

editNameActive: boolean = false;
editName(){
    this.editNameActive = this.editNameActive? false:true;
  }

set ElementRef, input element focus, when the element is show. Get input element value, when It's hide(not work).

private namePlaceholder: ElementRef;
  @ViewChild('namePlaceholder') set name(name: ElementRef){
    this.namePlaceholder = name;
    if(this.editNameActive) {
      this.namePlaceholder.nativeElement.focus();
    }else{
      //nativeElement' is undefined
      console.log(this.namePlaceholder.nativeElement.value);
    }
  }

I also try to get value in editName() before the status change:

editName(){
 if(this.editNameActive){
    console.log(this.namePlaceholder.nativeElement.value);
    this.editNameActive = false;
 }
}

But namePlaceholder work in name() only.

Finn
  • 1,323
  • 5
  • 24
  • 46

0 Answers0