focus via injected document

This commit is contained in:
tykayn 2019-08-10 21:00:05 +02:00
parent 54c7c6568d
commit d9b81d3d8e

View File

@ -1,7 +1,9 @@
import {ChangeDetectorRef, Component, OnInit} from '@angular/core'; import {ChangeDetectorRef, Component, Inject, OnInit} from '@angular/core';
import {BaseComponent} from '../base-page/base.component'; import {BaseComponent} from '../base-page/base.component';
import {ConfigService} from '../../config.service'; import {ConfigService} from '../../config.service';
import {DOCUMENT} from '@angular/common';
@Component({ @Component({
selector: 'framadate-answers', selector: 'framadate-answers',
templateUrl: './answers.component.html', templateUrl: './answers.component.html',
@ -12,6 +14,7 @@ export class AnswersComponent extends BaseComponent implements OnInit {
private answerList = []; private answerList = [];
constructor(config: ConfigService, constructor(config: ConfigService,
@Inject(DOCUMENT) private document: any,
private cd: ChangeDetectorRef) { private cd: ChangeDetectorRef) {
super(config); super(config);
this.answerList = this.config.answers; this.answerList = this.config.answers;
@ -32,8 +35,11 @@ export class AnswersComponent extends BaseComponent implements OnInit {
text: '' text: ''
}); });
this.cd.detectChanges(); // to refresh the view before focusing on the new input this.cd.detectChanges(); // to refresh the view before focusing on the new input
const AnswersDomToFocus = document.querySelectorAll('.answers .answer'); const AnswersDomToFocus = this.document.querySelectorAll('.answers .answer');
AnswersDomToFocus[AnswersDomToFocus.length - 1].focus(); const dom = AnswersDomToFocus[AnswersDomToFocus.length - 1];
if (dom.focus) {
dom.focus();
}
} }
// add a new answer on the press of ENTER in an input // add a new answer on the press of ENTER in an input