相关疑难解决方法(0)

Angular 5集合关注输入元素

我正在使用Angular 5进行前端应用程序,我需要隐藏一个搜索框,但点击一个按钮,搜索框将显示并设置焦点.

我已尝试使用指令左右的stackoverflow中找到的几种方法,但没有运气.

以下是示例代码:

@Component({
   selector: 'my-app',
   template: `
    <div>
    <h2>Hello</h2>
    </div>
    <button (click) ="showSearch()">Show Search</button>
    <p></p>
    <form>
      <div >
        <input *ngIf="show" #search type="text"  />            
      </div>
    </form>
    `,
  })
  export class App implements AfterViewInit {
  @ViewChild('search') searchElement: ElementRef;

  show: false;
  name:string;
  constructor() {    
  }

  showSearch(){
    this.show = !this.show;    
    this.searchElement.nativeElement.focus();
    alert("focus");
  }

  ngAfterViewInit() {
    this.firstNameElement.nativeElement.focus();
  }
Run Code Online (Sandbox Code Playgroud)

搜索框未设置为焦点.我怎样才能做到这一点?谢谢.

angular angular-forms angular5

50
推荐指数
10
解决办法
7万
查看次数

如何以编程方式将焦点设置为Angular2中动态创建的FormControl

我似乎无法在动态添加的FormGroup中将焦点设置在输入字段上:

addNewRow(){
    (<FormArray>this.modalForm.get('group1')).push(this.makeNewRow());
    // here I would like to set a focus to the first input field
    // say, it is named 'textField'

    // but <FormControl> nor [<AbstractControl>][1] dont seem to provide 
    // either a method to set focus or to access the native element
    // to act upon
}
Run Code Online (Sandbox Code Playgroud)

如何将焦点设置为angular2 FormControl或AbstractControl?

forms angular

18
推荐指数
3
解决办法
3万
查看次数

标签 统计

angular ×2

angular-forms ×1

angular5 ×1

forms ×1