In $stateProvider#state(), I define it as follows for Bootrtrap-ui modal using UI-Router. reference
var state = {
name: 'modala',
parent: 'home',
onEnter: function($modal, $state) {
modalInstance = $modal.open({
templateUrl: 'modala.html',
controller: 'modalaCtrl',
controllerAs: 'modal'
});
modalInstance.result['finaly'](function() {
modalInstance = null;
if ($state.$current.name === 'modala') {
$state.go('^');
}
});
},
onExit: function() {
if (modalInstance) {
modalInstance.close();
}
}
};
I want to use 'modala' any place other than home.
I do not want to create a lot of definitions of 'modala'
.
Is there a method to accept it, and to set what is necessary in parent?
add explanation
No good solutions
Don't set parent in modal state.
result: when open modal window, parent isn't displayed.
modal state name is ${parent.name}.modal format.
result: It's work. but, About one modal window, it is necessary to define many states. and, It is necessary to add a state whenever I add an parent who call modal.
define the modal state every parent
result:same as pattern 2.
All you have to do is add the property
parent: modala
to all child states that you define in order to set a parent state.I was wanting the solution to this too. I eventually worked it out.
See my stackoverflow answer / question
There is a solution built inside of the UI-Router, called state decorator
decorator(name, func)
This way, we can create an aspect, which will be injected into any state we want later. That aspect, could drive some common, global setting. E.g.
parent
orviews
...Check an example here: How to decorate current state resolve function in UI-Router? Current function isn't invoked, small cite:
There is a working example for our scenario
What we would need, is the decorator definition, like this:
Check that in action here
There is updated plunker (the second try from the question)
What we would see in action, is different use of decorator, as in detail described in my previous post
So, firstly, let's have few states e.g. 'home', 'page1', 'page2' ... and we would like for all of them introduce the child state - with modal functionality. That modal features are expected to be the same across all child states - but these will belong to different parents.
To make it happen we can use state definition like this:
And that could be enough, if we will hijack the decorator this way:
We extended every child state which name endsWith
".generalModal"
withonExit
andonEnter
. That's all. At one place...Check it in action here