【发布时间】:2018-01-07 08:35:34
【问题描述】:
**已更新**,解决方案接近工作:
我正在尝试让我的“跳转到内容”链接跳转到“#content-start”之后的第一个可聚焦元素。
登录-base.component.ts:
import { Component, OnInit } from '@angular/core';
import { Inject, Injectable } from '@angular/core';
import { DOCUMENT } from '@angular/platform-browser';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss']
})
export class BaseLoginComponent implements OnInit {
constructor(
@Inject(DOCUMENT) private document: any
) {
}
skip() {
console.log(document);
console.log(document.querySelector);
var content = document.querySelector(`#content-start`); // ?? document is null!!
if (content) {
var control = content.querySelector('input, button, a');
console.log(control);
//if (control) {
// control.focus(); // first only
//}
}
};
ngOnInit() {
this.document.getElementById('skipLink').onclick = this.skip;
this.document.getElementById('skipLink').onkeypress = function (e) {
if (e.keyCode == 13) {
this.skip();
}
}
}
}
login.component.html:
<div class="login-page">
<section class="main container" id="content-start">
<input type="text"/>
这确实有效,因为 console.log(control) 返回
<input _ngcontent-c4="" id="username" name="username">
这实际上是正确的控制。
但我不能只设置 .focus(),因为我拥有的是 HTML sn-p,而不是带有方法的对象,例如 .focus();
呃,有 jQuery 的 $(control) 的 Angular 等价物吗?
【问题讨论】:
-
离题。
@Component扩展自@Injectable,无需使用@Injectable注释您的组件 -
stackoverflow.com/questions/37521298/… 也许这将有助于假设您的文档注入不起作用
标签: angular selectors-api