繁体   English   中英

纸张输入自动对焦在角度2组件的路由器插座内部不起作用

[英]paper-input autofocus doesn't work inside router-outlet in an angular 2 component

假设我在angular 2应用程序中定义了以下路由:

@RouteConfig([{
        component: FirstRouteComponent,
        name: "First",
        path: "/First",
        useAsDefault: true
    },
    {
        component: SecondRouteComponentComponent,
        name: "Second",
        path: "/Second"
    }])
    export class AppComponent {
    }

在FirstRouteComponent模板中,我有:

<paper-input autofocus></paper-input>

当我从路线“Second”导航到路线“First”(通过某些[routerLink]指令,我在页面中的某个静态标签上)时,纸张输入没有聚焦。 奇怪的是,如果我已经在路线“第一”并且我刷新页面,则纸张输入成功聚焦。

我有一种感觉这与角度2路由器动态地将纸张输入插入dom的方式有关,以及聚合物如何使用自动对焦但我无法指出问题。 我可能错了,这个问题很“奇怪”,因为我之前在组件中将任何其他参数传递给聚合物组件(包括纸张输入)没有任何问题。

在此先感谢任何帮助:)

你可以写:

<paper-input #autofocus></paper-input>
in AppComponent:

import { AfterViewInit, ViewChildren } from '@angular/core';
 export class AppComponent implements  AfterViewInit {
   @ViewChildren('autofocus') fc;
   ngAfterViewInit() {
        this.fc.first.nativeElement.focus();
    }
}

暂无
暂无

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

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