[英]how to focus on a text-box cross component in angular
首先,您必须将路由添加到您的 Child2(假设它是 class 名称),以便当用户单击 header 上的按钮时,它会被路由到 Child2。
1.<button class="header" [routerLink]="['Child2']">Child2</button>
//Child2 is the name of child2 Component
2.然后使用 Angular 生命周期钩子 -> ngOnInit() {} ngOnInit 是由 Angular 调用的生命周期钩子,表示 ZC31C335EF37283C451B18BA0DD317DE1 正在创建组件。 并放置一些 typescript 代码以使文本框成为焦点。
<input type="text"><a href="#textbox"></a>Go to bottom</input>
<input type="text" id="textbox"></footer>
您可以在上面的示例中构建一些方法。 或者
document.getElementById("textbox").click();
这将使焦点进入组件 child2 中存在的文本框
这是您如何实现此功能的方法
app.module.ts
提供者SharedService
注入 child-one-component.ts 并调用此服务的setData()
方法,我们在 observable 中设置值SharedService
注入 child-two-component.ts 并调用服务的getData()
方法onInit()
将在您单击setData()
时同时订阅 observableapp.component.html
<app-child-one></app-child-one>
<app-child-two></app-child-two>
儿童一号.html
<div style="border: 2px solid black; padding:15px; margin-bottom:3px">
<p>child-one </p>
<button (click)="setFocus()">Set Focus</button>
<button (click)="removeFocus()" style="margin-left:10px;">Remove Focus</button>
</div>
小孩子.ts
import { Component, OnInit, EventEmitter } from '@angular/core';
import { SharedService } from '../shared.service';
@Component({
selector: 'app-child-one',
templateUrl: './child-one.component.html',
styleUrls: ['./child-one.component.css']
})
export class ChildOneComponent implements OnInit {
constructor(private sharedService: SharedService) { }
ngOnInit() {
}
setFocus() {
this.sharedService.setData(true);
}
removeFocus() {
this.sharedService.setData(false);
}
}
孩子二.html
<div style="border: 2px solid black; padding:25px 15px;">
<p>child-two </p>
<input type="text" placeholder="Enter text" #input/>
</div>
child-two.ts
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { SharedService } from '../shared.service';
@Component({
selector: 'app-child-two',
templateUrl: './child-two.component.html',
styleUrls: ['./child-two.component.css']
})
export class ChildTwoComponent implements OnInit {
@ViewChild('input', {static: true}) private input: ElementRef;
constructor(private sharedService: SharedService) { }
ngOnInit() {
// will get called when user click on Set Focus button
this.sharedService.getData().subscribe((response: any)=> {
if(response) {
this.input.nativeElement.focus(); // will set focus into input here
} else {
this.input.nativeElement.blur();
}
})
}
}
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { ChildOneComponent } from './child-one/child-one.component';
import { ChildTwoComponent } from './child-two/child-two.component';
import { SharedService } from './shared.service';
@NgModule({
imports: [ BrowserModule, FormsModule ],
declarations: [ AppComponent, ChildOneComponent, ChildTwoComponent ],
bootstrap: [ AppComponent ],
providers: [SharedService]
})
export class AppModule { }
shared.service.ts将使用此服务将消息从一个组件发送到另一个组件。
import { Injectable } from '@angular/core';
import { Observable, Subject } from 'rxjs';
@Injectable()
export class SharedService {
private subject$ = new Subject<any>();
constructor() { }
setData(data) {
this.subject$.next(data);
}
getData() {
return this.subject$.asObservable();
}
}
这是工作演示https://stackblitz.com/edit/santosh-angular-set-focus-in-input-from-another-component
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.