[英]Angular2 Dialog Callback
All, I have an Angular2 application. 全部,我有一个Angular2应用程序。 In this application I have a button that opens a dialog.
在此应用程序中,我有一个打开对话框的按钮。 In this dialog is a single div which I need to act on.
在此对话框中,我需要操作一个div。 Is there any way to execute code in a callback after the dialog and all it's children have been created?
对话框及其所有子级创建之后,是否可以在回调中执行代码?
The closest I've got is 我最近的是
<modal #openDialog (focusin)="openExplorer('someData')">
...
</modal>
If I go with that approach then my code get's rexecuted if a user clicks on the form fields inside of this div. 如果我采用这种方法,那么如果用户单击该div内的表单字段,则会重新执行我的代码。
// HTML
<p (click)="openTheDialog(openDialog, 'thisDivRightHere')">Click here to open me, fool</p>
<modal #openDialog (focusin)="openExplorer('someData')">
<div id="thisDivRightHere"></div>
</modal>
// Component
openTheDialog(dlg, someDiv) void: {
this.fooBarService.preformTheAction(someDiv);
}
// Service
preformTheAction = function(someDiv) {
$('#' + someDiv).html(new Date());
}
In the above example, without (focusin) then I can't get a handle on "thisDivRightHere" 在上面的示例中,如果没有(focusin),则无法获得“ thisDivRightHere”的句柄
But if I use (focusin) I can but the date changes every time somethings interacted with. 但是,如果我使用(focusin),则每次进行交互时,日期都会更改。
I solved it by using setTimeout to wait for the element and a carefully placed callback 我通过使用setTimeout等待元素和精心放置的回调来解决它
// HTML
<p (click)="openTheDialog(openDialog)">Click here to open me, fool</p>
<modal #openDialog (onOpen)="openIt(openDialog, 'thisDivRightHere')" >
<div id="thisDivRightHere"></div>
</modal>
tester(someDiv, dialog, callback): void {
var checkExist = setInterval(function() {
if ($('#'+someDiv).length) {
console.log("Exists!");
clearInterval(checkExist);
callback(dialog);
}
}, 100); // check every 100ms
}
// Component
openTheDialog(dlg) {
dlg.open();
}
openIt(dlg, someDiv) void: {
this.tester(someDiv, this.myService, function(dlg) {
dialog.preformTheAction(someDiv);
});
}
// Service
preformTheAction = function(someDiv) {
$('#' + someDiv).html(new Date());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.