I have the following logic in my program:
- User clicks some button.
- Modal dialog appears.
- Once it is closed, promise is returned (I am using $modal from angular-bootstrap) and some action happens.
so it is simple call of $modal:
var modalInstance = $modal.open({
// here goes modal configuration
});
modalInstance.result.then(function (chosenProject) {
// some action on data returned from modal
})
What I need is to $broadcast an event BEFORE I am about to open that dialog from #2. An "intermediary" code, subscribed to that event, should affect actions at #2-3, namely, be able to prevent execution of these steps. So, I am willing to have such execution plan:
- User clicks some button
- $broadcast("aboutToOpenDialog")
- a function subscribed to event aboutToOpenDialog is called and makes some decisions and returns a "result".
- This "result" somehow returned to code that executed $broadcast (some promise, perhaps).
- If "result" is "OK", then open modal dialog, if "result" is "ERROR" - don't proceed.
The code will start looking as:
$rootScope.$broadcast("aboutToOpenDialog");
/// !!! Then I need some function to listen to the event, make decision, return some data,
/// and I want then use that data to decide, if I open dialog or not:
var modalInstance = $modal.open({
// here goes modal configuration
});
modalInstance.result.then(function (chosenProject) {
// some action on data returned from modal
})
How I can code such event-driven sequence using angular?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…