使用承诺捕获关闭
Capturing the close using promises
我有一个问题,因为我需要在 ui-bootstrap 模式关闭(并完成动画)时执行一个方法。我不知道如何正确地写下承诺。到目前为止我有这个(在 TypeScript 中):
public open(attrs, opts) {
var scope = this.rootScope.$new();
angular.extend(scope, attrs);
opts = angular.extend(opts || {}, {
backdrop: false,
scope: scope,
templateUrl: 'splash/content.html',
windowTemplateUrl: 'splash/index.html'
});
this.modalInstance = this.modal.open(opts);
this.modalInstance.result.then(
function() {
console.log("1");
},
function() {
console.log("2");
},
function() {
console.log("3");
});
}
public close() {
if (this.modalInstance != null && this.modalInstance != undefined) {
this.modalInstance.close();
}
}
我已经尝试将 .result 附加到 close 方法,但显然它不会退出:似乎只有 open 方法 returns 是一个承诺。谁能帮我吗?控制台中只显示“1”,我认为这是打开的解决方案。
angular-ui-bootstrap 模态结果 属性 returns 您发现的承诺。如果关闭模态框 (confirmed/yes),承诺将得到解决,但如果关闭模态框(在 modal/cancel 外部单击),承诺将被拒绝。
如果您想在模式关闭后调用多个函数,您可以将它们与额外的 .then() 调用链接在一起。
您可以使用 .catch() 查看模态框是否被取消/关闭。
this.modalInstance.result.then(function() {
console.log(1);
}).then(function() {
console.log(2);
}).then(function() {
console.log(3);
}).catch(function(err) {
console.log('the modal was dismissed so the promise chain was rejected');
});
我有一个问题,因为我需要在 ui-bootstrap 模式关闭(并完成动画)时执行一个方法。我不知道如何正确地写下承诺。到目前为止我有这个(在 TypeScript 中):
public open(attrs, opts) {
var scope = this.rootScope.$new();
angular.extend(scope, attrs);
opts = angular.extend(opts || {}, {
backdrop: false,
scope: scope,
templateUrl: 'splash/content.html',
windowTemplateUrl: 'splash/index.html'
});
this.modalInstance = this.modal.open(opts);
this.modalInstance.result.then(
function() {
console.log("1");
},
function() {
console.log("2");
},
function() {
console.log("3");
});
}
public close() {
if (this.modalInstance != null && this.modalInstance != undefined) {
this.modalInstance.close();
}
}
我已经尝试将 .result 附加到 close 方法,但显然它不会退出:似乎只有 open 方法 returns 是一个承诺。谁能帮我吗?控制台中只显示“1”,我认为这是打开的解决方案。
angular-ui-bootstrap 模态结果 属性 returns 您发现的承诺。如果关闭模态框 (confirmed/yes),承诺将得到解决,但如果关闭模态框(在 modal/cancel 外部单击),承诺将被拒绝。
如果您想在模式关闭后调用多个函数,您可以将它们与额外的 .then() 调用链接在一起。
您可以使用 .catch() 查看模态框是否被取消/关闭。
this.modalInstance.result.then(function() {
console.log(1);
}).then(function() {
console.log(2);
}).then(function() {
console.log(3);
}).catch(function(err) {
console.log('the modal was dismissed so the promise chain was rejected');
});