From d9b81d3d8ef5001fd7e199afcd05051263b21973 Mon Sep 17 00:00:00 2001 From: tykayn Date: Sat, 10 Aug 2019 21:00:05 +0200 Subject: [PATCH] focus via injected document --- src/app/pages/answers/answers.component.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/app/pages/answers/answers.component.ts b/src/app/pages/answers/answers.component.ts index a17b1a15..97071057 100644 --- a/src/app/pages/answers/answers.component.ts +++ b/src/app/pages/answers/answers.component.ts @@ -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 {ConfigService} from '../../config.service'; +import {DOCUMENT} from '@angular/common'; + @Component({ selector: 'framadate-answers', templateUrl: './answers.component.html', @@ -12,6 +14,7 @@ export class AnswersComponent extends BaseComponent implements OnInit { private answerList = []; constructor(config: ConfigService, + @Inject(DOCUMENT) private document: any, private cd: ChangeDetectorRef) { super(config); this.answerList = this.config.answers; @@ -32,8 +35,11 @@ export class AnswersComponent extends BaseComponent implements OnInit { text: '' }); this.cd.detectChanges(); // to refresh the view before focusing on the new input - const AnswersDomToFocus = document.querySelectorAll('.answers .answer'); - AnswersDomToFocus[AnswersDomToFocus.length - 1].focus(); + const AnswersDomToFocus = this.document.querySelectorAll('.answers .answer'); + const dom = AnswersDomToFocus[AnswersDomToFocus.length - 1]; + if (dom.focus) { + dom.focus(); + } } // add a new answer on the press of ENTER in an input