在按钮单击时,我有 3 个函数一个接一个地被调用。在第一个函数中,我想要阻塞模型窗口,它将在该窗口上返回一个承诺,并且在该承诺上我将解决下一个函数。
html代码是:
<div>
<h2>Hello {{name}}</h2>
<button (click)="OnAgeAYear()">click me</button>
<div bsModal #getRichModal="bs-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" aria-label="Close" (click)="hideChildModal()">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title">Child modal</h4>
</div>
<div class="modal-body">
getRich quick
</div>
<button type="button" class="btn btn-primary" (click)="confirmGetRichYes()" >YES</button>
<button type="button" class="btn btn-primary" (click)="confirmGetRichNo()" >NO</button>
</div>
</div>
</div>
</div>
按钮点击功能:
OnAgeAYear() {
//this.getRichModal.show();
this.fateGetRichQuick()
.then(
result=>{
console.log("promise returned: " + result);
return this.fateCharity();
},
error=> console.log(error))
.then(
result=>{
console.log("promise returned: " + result);
return this.fateMarriage();
}).then(
result=>{
console.log("promise returned: " + result);
return this.fateMarriage();
}
)
.catch(error => {
console.log("generic cach "+error); // will be called if getUser or getRights fails
})}
3个功能是:
fateGetRichQuick(): Promise<any>{
var modal = this.getRichModal;
return new Promise<any>(function (resolve, reject) {
var n= 1;
modal.show();
// I want to resolve this promise only when user clicked yes or no.
});}
fateCharity(): Promise<any> {
return new Promise<any>(function (resolve, reject) {
resolve("fateCharity-Resolve");
})}
fateMarriage(): Promise<any> {
return new Promise<any>(function (resolve, reject) {
resolve("fateMarriage - Resolve");
})}
在模态窗口上我有以下功能:
confirmGetRichYes(): Promise <boolean>{
alert("it is yes....");
this.hideChildModal();
return Promise.resolve(true);}
confirmGetRichNo(): Promise <boolean>{
alert("it is no....");
this.hideChildModal();
return Promise.resolve(false);}
hideChildModal(){
this.getRichModal.hide();}
所以在confirmGetRichYes上,我想解决fateGetRichQuick承诺,但模态窗口不会阻止功能fateGetRichQuick,而是继续它的流程。
我在 plunker 上运行代码:http: //plnkr.co/edit/nX1LcNJeuQvuWSloF50l