在 onRemoving 回调中获取 Button 值
Get Button value in onRemoving callback
我正在使用 AngularMaterial 的 $mdDialog 服务。这是我正在使用的示例代码 -
var highlighter = {};
$mdDialog.show({
templateUrl: 'sample.html',
parent: angular.element(document.body),
clickOutsideToClose: true,
bindToController: true,
onRemoving: function (event, removePromise) {
if(highlighter.answer!==1) {
//Do something here
}
highlighter.answer = undefined;
}
})
.then(function (answer) {
highlighter.answer = answer;
});
仅当单击 mdDialog 中的任何按钮时,show()
方法返回的承诺才会解析。按钮的索引作为 answer
参数提供。只有当 answer/button 索引不等于 1 时,我才需要做一些事情。
问题是在单击任何按钮时在解决承诺之前调用 onRemoving,因此 highlighter.answer
在需要时没有获得正确的值(在 onRemoving 回调中)。
换句话说,只要调用任何按钮或在不单击按钮的情况下关闭对话框,就会调用 onRemoving。只有单击按钮时,承诺才会得到解决。
所以我的问题是,有没有什么方法可以在 promise 解决(然后被调用)后调用任何回调,以便设置答案值并且我知道是否调用了按钮.无论承诺是否已解决,都应在删除对话框时调用此回调。
我忘了 mdDialog
使用 q
库来实现承诺,它也有一个 finally
动作。我只需要添加 finally
操作并将 onRemoving
代码放在那里。然后我可以删除 onRemoving
侦听器。最终代码如下所示 -
$mdDialog.show({
templateUrl: 'app/modules/hire/common/views/annotation.html',
parent: angular.element(document.body),
clickOutsideToClose: true,
bindToController: true
}).then(function (answer) {
highlighter.answer = answer;
}).finally(function () {
if(highlighter.answer!==1) {
//Do something here
}
highlighter.answer = undefined;
});
帮助我找到了解决方案。
我正在使用 AngularMaterial 的 $mdDialog 服务。这是我正在使用的示例代码 -
var highlighter = {};
$mdDialog.show({
templateUrl: 'sample.html',
parent: angular.element(document.body),
clickOutsideToClose: true,
bindToController: true,
onRemoving: function (event, removePromise) {
if(highlighter.answer!==1) {
//Do something here
}
highlighter.answer = undefined;
}
})
.then(function (answer) {
highlighter.answer = answer;
});
仅当单击 mdDialog 中的任何按钮时,show()
方法返回的承诺才会解析。按钮的索引作为 answer
参数提供。只有当 answer/button 索引不等于 1 时,我才需要做一些事情。
问题是在单击任何按钮时在解决承诺之前调用 onRemoving,因此 highlighter.answer
在需要时没有获得正确的值(在 onRemoving 回调中)。
换句话说,只要调用任何按钮或在不单击按钮的情况下关闭对话框,就会调用 onRemoving。只有单击按钮时,承诺才会得到解决。
所以我的问题是,有没有什么方法可以在 promise 解决(然后被调用)后调用任何回调,以便设置答案值并且我知道是否调用了按钮.无论承诺是否已解决,都应在删除对话框时调用此回调。
我忘了 mdDialog
使用 q
库来实现承诺,它也有一个 finally
动作。我只需要添加 finally
操作并将 onRemoving
代码放在那里。然后我可以删除 onRemoving
侦听器。最终代码如下所示 -
$mdDialog.show({
templateUrl: 'app/modules/hire/common/views/annotation.html',
parent: angular.element(document.body),
clickOutsideToClose: true,
bindToController: true
}).then(function (answer) {
highlighter.answer = answer;
}).finally(function () {
if(highlighter.answer!==1) {
//Do something here
}
highlighter.answer = undefined;
});