Angular/PrimeNg: Focus on input each time I open a p-dialog











up vote
2
down vote

favorite












I'd like to auto focus a certain input field which is in a p-dialog component,
which is opened/closed by a flag that is bind to the dialog's visible attribute.



When I add an "autofocus" attribute to the input tag - it is focused only in the first time the dialog is opened.



When I use the "onShow" event - it is emitted BEFORE the dialog is open.



When I add an "autofocus" directive and try to set the focus in AfterViewInit - it is called only once.



Anyone knows how I can set the focus or get a "onAfterShow" event for the dialog?










share|improve this question
























  • expecting some code to solve your issue - event when dialog triggered and the input you want focus when dialog is shown
    – Rahul Swamynathan
    Nov 8 at 10:09















up vote
2
down vote

favorite












I'd like to auto focus a certain input field which is in a p-dialog component,
which is opened/closed by a flag that is bind to the dialog's visible attribute.



When I add an "autofocus" attribute to the input tag - it is focused only in the first time the dialog is opened.



When I use the "onShow" event - it is emitted BEFORE the dialog is open.



When I add an "autofocus" directive and try to set the focus in AfterViewInit - it is called only once.



Anyone knows how I can set the focus or get a "onAfterShow" event for the dialog?










share|improve this question
























  • expecting some code to solve your issue - event when dialog triggered and the input you want focus when dialog is shown
    – Rahul Swamynathan
    Nov 8 at 10:09













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I'd like to auto focus a certain input field which is in a p-dialog component,
which is opened/closed by a flag that is bind to the dialog's visible attribute.



When I add an "autofocus" attribute to the input tag - it is focused only in the first time the dialog is opened.



When I use the "onShow" event - it is emitted BEFORE the dialog is open.



When I add an "autofocus" directive and try to set the focus in AfterViewInit - it is called only once.



Anyone knows how I can set the focus or get a "onAfterShow" event for the dialog?










share|improve this question















I'd like to auto focus a certain input field which is in a p-dialog component,
which is opened/closed by a flag that is bind to the dialog's visible attribute.



When I add an "autofocus" attribute to the input tag - it is focused only in the first time the dialog is opened.



When I use the "onShow" event - it is emitted BEFORE the dialog is open.



When I add an "autofocus" directive and try to set the focus in AfterViewInit - it is called only once.



Anyone knows how I can set the focus or get a "onAfterShow" event for the dialog?







angular angular6 primeng






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 10:12









Jasper de Vries

8,51953472




8,51953472










asked Nov 8 at 10:06









Tal Sheffer

405




405












  • expecting some code to solve your issue - event when dialog triggered and the input you want focus when dialog is shown
    – Rahul Swamynathan
    Nov 8 at 10:09


















  • expecting some code to solve your issue - event when dialog triggered and the input you want focus when dialog is shown
    – Rahul Swamynathan
    Nov 8 at 10:09
















expecting some code to solve your issue - event when dialog triggered and the input you want focus when dialog is shown
– Rahul Swamynathan
Nov 8 at 10:09




expecting some code to solve your issue - event when dialog triggered and the input you want focus when dialog is shown
– Rahul Swamynathan
Nov 8 at 10:09












1 Answer
1






active

oldest

votes

















up vote
0
down vote













I use a setTimeout to set the focus of element after specific time base on the form appearance as example 500 millisecond



template



<p-dialog [header]="dialogTitle" [(visible)]="isDialogVisible" [responsive]="true" 
[appendTo]="'body'" (onShow)="setFocus(txtElement)">
<input #txtElement >
</p-dialog>


component



setFocus(elm: HTMLInputElement) {
setTimeout(() => {
elm.focus()
}, 500);
}





share|improve this answer





















  • I did something similar, but: (1) it is not a "clean" solution (2) sometimes my app is heavy and the timeout may not be enough. Isn't there a more elegant solution to this problem? I assume it is a main-stream use case...
    – Tal Sheffer
    Nov 8 at 10:21












  • the do have a proprty for button focusOnShow so I guess we need to ask for another proprty like focusOnShowElement so the best case is to open an issue on github repo
    – malbarmawi
    Nov 8 at 10:34












  • check this stackoverflow.com/questions/15247849/… onShow dons't work like showing I think
    – malbarmawi
    Nov 8 at 10:37













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205456%2fangular-primeng-focus-on-input-each-time-i-open-a-p-dialog%23new-answer', 'question_page');
}
);

Post as a guest
































1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













I use a setTimeout to set the focus of element after specific time base on the form appearance as example 500 millisecond



template



<p-dialog [header]="dialogTitle" [(visible)]="isDialogVisible" [responsive]="true" 
[appendTo]="'body'" (onShow)="setFocus(txtElement)">
<input #txtElement >
</p-dialog>


component



setFocus(elm: HTMLInputElement) {
setTimeout(() => {
elm.focus()
}, 500);
}





share|improve this answer





















  • I did something similar, but: (1) it is not a "clean" solution (2) sometimes my app is heavy and the timeout may not be enough. Isn't there a more elegant solution to this problem? I assume it is a main-stream use case...
    – Tal Sheffer
    Nov 8 at 10:21












  • the do have a proprty for button focusOnShow so I guess we need to ask for another proprty like focusOnShowElement so the best case is to open an issue on github repo
    – malbarmawi
    Nov 8 at 10:34












  • check this stackoverflow.com/questions/15247849/… onShow dons't work like showing I think
    – malbarmawi
    Nov 8 at 10:37

















up vote
0
down vote













I use a setTimeout to set the focus of element after specific time base on the form appearance as example 500 millisecond



template



<p-dialog [header]="dialogTitle" [(visible)]="isDialogVisible" [responsive]="true" 
[appendTo]="'body'" (onShow)="setFocus(txtElement)">
<input #txtElement >
</p-dialog>


component



setFocus(elm: HTMLInputElement) {
setTimeout(() => {
elm.focus()
}, 500);
}





share|improve this answer





















  • I did something similar, but: (1) it is not a "clean" solution (2) sometimes my app is heavy and the timeout may not be enough. Isn't there a more elegant solution to this problem? I assume it is a main-stream use case...
    – Tal Sheffer
    Nov 8 at 10:21












  • the do have a proprty for button focusOnShow so I guess we need to ask for another proprty like focusOnShowElement so the best case is to open an issue on github repo
    – malbarmawi
    Nov 8 at 10:34












  • check this stackoverflow.com/questions/15247849/… onShow dons't work like showing I think
    – malbarmawi
    Nov 8 at 10:37















up vote
0
down vote










up vote
0
down vote









I use a setTimeout to set the focus of element after specific time base on the form appearance as example 500 millisecond



template



<p-dialog [header]="dialogTitle" [(visible)]="isDialogVisible" [responsive]="true" 
[appendTo]="'body'" (onShow)="setFocus(txtElement)">
<input #txtElement >
</p-dialog>


component



setFocus(elm: HTMLInputElement) {
setTimeout(() => {
elm.focus()
}, 500);
}





share|improve this answer












I use a setTimeout to set the focus of element after specific time base on the form appearance as example 500 millisecond



template



<p-dialog [header]="dialogTitle" [(visible)]="isDialogVisible" [responsive]="true" 
[appendTo]="'body'" (onShow)="setFocus(txtElement)">
<input #txtElement >
</p-dialog>


component



setFocus(elm: HTMLInputElement) {
setTimeout(() => {
elm.focus()
}, 500);
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 8 at 10:19









malbarmawi

4,45031131




4,45031131












  • I did something similar, but: (1) it is not a "clean" solution (2) sometimes my app is heavy and the timeout may not be enough. Isn't there a more elegant solution to this problem? I assume it is a main-stream use case...
    – Tal Sheffer
    Nov 8 at 10:21












  • the do have a proprty for button focusOnShow so I guess we need to ask for another proprty like focusOnShowElement so the best case is to open an issue on github repo
    – malbarmawi
    Nov 8 at 10:34












  • check this stackoverflow.com/questions/15247849/… onShow dons't work like showing I think
    – malbarmawi
    Nov 8 at 10:37




















  • I did something similar, but: (1) it is not a "clean" solution (2) sometimes my app is heavy and the timeout may not be enough. Isn't there a more elegant solution to this problem? I assume it is a main-stream use case...
    – Tal Sheffer
    Nov 8 at 10:21












  • the do have a proprty for button focusOnShow so I guess we need to ask for another proprty like focusOnShowElement so the best case is to open an issue on github repo
    – malbarmawi
    Nov 8 at 10:34












  • check this stackoverflow.com/questions/15247849/… onShow dons't work like showing I think
    – malbarmawi
    Nov 8 at 10:37


















I did something similar, but: (1) it is not a "clean" solution (2) sometimes my app is heavy and the timeout may not be enough. Isn't there a more elegant solution to this problem? I assume it is a main-stream use case...
– Tal Sheffer
Nov 8 at 10:21






I did something similar, but: (1) it is not a "clean" solution (2) sometimes my app is heavy and the timeout may not be enough. Isn't there a more elegant solution to this problem? I assume it is a main-stream use case...
– Tal Sheffer
Nov 8 at 10:21














the do have a proprty for button focusOnShow so I guess we need to ask for another proprty like focusOnShowElement so the best case is to open an issue on github repo
– malbarmawi
Nov 8 at 10:34






the do have a proprty for button focusOnShow so I guess we need to ask for another proprty like focusOnShowElement so the best case is to open an issue on github repo
– malbarmawi
Nov 8 at 10:34














check this stackoverflow.com/questions/15247849/… onShow dons't work like showing I think
– malbarmawi
Nov 8 at 10:37






check this stackoverflow.com/questions/15247849/… onShow dons't work like showing I think
– malbarmawi
Nov 8 at 10:37




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205456%2fangular-primeng-focus-on-input-each-time-i-open-a-p-dialog%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

Landwehr

Reims

Schenkenzell