angular2 template - Autofocus not working in angular 4 -
i need autofocus element inside ngfor loop.
here chat.component.html
<div *ngfor="let chat of chats; let last = last"> {{ chat.chat }} <span *ngif="last;" autofocus></span> </div> actualy, using button click i'm showing chat popup, need autofocus last chat.
my chat.ts file
import { component, oninit } '@angular/core'; import { authservice } '../../services/auth.service'; import * io "socket.io-client"; @component({ selector: 'app-lsidebar', templateurl: './lsidebar.component.html', styleurls: ['./lsidebar.component.css'] }) export class lsidebarcomponent implements oninit { chat:string; showchat:boolean; chats:any[]; fanmateid:string; socket = io('http://localhost:3000'); constructor(private authservice:authservice) { } ngoninit() { this.showfans = false; this.showchat = false; this.getfanmates(); this.chatss(); } getchat(fanmateid,name){ this.showchat = !this.showchat; this.chatname = name; this.fanmateid = fanmateid; const temp = { fanmate_id: fanmateid } this.getchats(temp); } getchats(temp){ this.chats = []; this.authservice.getchat(temp); } chatss(){ this.socket.on('chatres',function(chats){ this.chats = chats.chats[0].messages; }.bind(this)); this.socket.on('addchat',function(chats){ this.chat = ''; // console.log(chats); }.bind(this)); } } is there suggestions end search on autofocus on google angular 4.
autofocus attribute works on <button>, <input>, <keygen>, <select> , <textarea>. read documentation.
you need set tabindex attribute on span , focus manually in ngafterviewinit().
<div *ngfor="let chat of chats; let last = last"> {{ chat.chat }} <span *ngif="last;" #lastchat tabindex="9999"></span> </div> in ts code:
@viewchild('lastchat') lastchat:htmlelement; ngafterviewinit(){ this.lastchat.nativeelement.focus(); } link working demo.
Great insights shared here! Businesses today need smarter ways to manage marketing tasks without increasing overhead, and a virtual marketing assistant can make a real difference. A professional virtual marketing assistant helps with social media management, email campaigns, lead generation, content scheduling, and market research, allowing business owners to focus on growth. Meet My Assistant provides reliable and skilled support tailored to your marketing needs. With the right virtual support, companies can stay consistent, organized, and competitive in today’s digital landscape while saving both time and resources. Highly recommended for businesses aiming to scale efficiently.
ReplyDelete