简体   繁体   English

如何专注于 angular 中的文本框交叉组件

[英]how to focus on a text-box cross component in angular

i have a page with main parent element and two child elements quite like this.我有一个包含主要父元素和两个子元素的页面,非常像这样。 在此处输入图像描述

how can i focus on the text box in the child2 component, when clicked on the button on child 1 component.当单击子 1 组件上的按钮时,如何专注于子 2 组件中的文本框。

child 1 component is actually the header section of my page. child 1 组件实际上是我页面的 header 部分。 so there may also be cases like child1 is always visible, but instead of child2, there is some different component on display.所以也可能有像 child1 总是可见的情况,但不是 child2,而是显示了一些不同的组件。 then when i click on the button on child1, it should route to child2 and then scroll down and focus on the required text box of child 2..然后当我单击 child1 上的按钮时,它应该路由到 child2,然后向下滚动并专注于 child 2 所需的文本框。

how to implement this function in angular.如何在 angular 中实现这个 function。

First of All you have to add the routing to your Child2(Assuming it as a class name) So that when user clicks the button on your header it is routed to Child2.首先,您必须将路由添加到您的 Child2(假设它是 class 名称),以便当用户单击 header 上的按钮时,它会被路由到 Child2。

 1.<button class="header" [routerLink]="['Child2']">Child2</button>
//Child2 is the name of child2 Component

2.And then use Angular life cycle hooks -> ngOnInit() {} ngOnInit is a life cycle hook called by Angular to indicate that the Angular is done creating the component. 2.然后使用 Angular 生命周期钩子 -> ngOnInit() {} ngOnInit 是由 Angular 调用的生命周期钩子,表示 ZC31C335EF37283C451B18BA0DD317DE1 正在创建组件。 And place some typescript code to make the textbox in focus.并放置一些 typescript 代码以使文本框成为焦点。

<input type="text"><a href="#textbox"></a>Go to bottom</input>

<input type="text" id="textbox"></footer>

You can build some approach over the above example.您可以在上面的示例中构建一些方法。 or或者

document.getElementById("textbox").click();

This will bring in focus into the textbox that is present in the component child2这将使焦点进入组件 child2 中存在的文本框

Here is how you can implement this这是您如何实现此功能的方法

  1. Create one service will have to methods创建一个服务必须要方法
  2. Put in app.module.ts providers放入app.module.ts提供者
  3. Now inject this SharedService to child-one-component.ts and call setData() method of this service where we are setting value in observable现在将此SharedService注入 child-one-component.ts 并调用此服务的setData()方法,我们在 observable 中设置值
  4. Now inject same SharedService to child-two-component.ts and call getData() method of service onInit() will subscribe to observable at the same time when you click on setData()现在将相同的SharedService注入 child-two-component.ts 并调用服务的getData()方法onInit()将在您单击setData()时同时订阅 observable

app.component.html app.component.html

<app-child-one></app-child-one>
<app-child-two></app-child-two>

child-one.html儿童一号.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>

child-one.ts小孩子.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);
  }

}

child-two.html孩子二.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 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 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 will use this service to send message from one component to another. 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();
  }
}

Here is working demo https://stackblitz.com/edit/santosh-angular-set-focus-in-input-from-another-component这是工作演示https://stackblitz.com/edit/santosh-angular-set-focus-in-input-from-another-component

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM