In my application I have 2 pages. I have a button on my first page which when clicked routes to my second page:
<a href="#/second-page" class="btn btn-default" >
On my second page I want to show my modal popup as soon as we load the second page. For this I am using the following code in my second controller to show the popup.
var modalInstance = $modal.open({
templateUrl: myUrl + 'app/page/modal.html',
controller: 'secondPageCtrl',
backdrop: 'static',
keyboard: false
});
When someone press cancel on the modal, I want to close the modal and then go back to my first page:
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
$state.go('first-page');
};
All this works fine. The issue is now after the above redirect when I press the button on my first page again, the modal popup is not open by default on page load of second page. Looks like its hidden and the controller code of opening the modal is not triggered.
Is there something else I need to add.
-----------Updated--------------
Here is my plunkr http://jsfiddle.net/b9y2Lc5x/
I found out the issue but not the solution. The issue is I am using factory as my angular service & I had defined my modal in that service. In my controller I was calling my service as:
$scope.mymodal = myService.createModal;
And in my serivice there is no createModal variable/method. Not sure why its not giving error in my application but gave error in my jsfiddle.
So now 2 questions here: - How do I define createModal in my service so that it can be called from my controller. - Is this the correct way to call a modal ie from controller to service. Or should I move the logic of opening modal in my controller.
href="#/second-page"withui-sref="second-page"