How do I open a modal from inside a tab's view-title, ion-nav-buttons? for example:
<ion-view view-title="Users">
<ion-nav-buttons side="primary">
<button class="button button-icon icon ion-ios-minus-outline"
ng-click="openModal()"></button>
</ion-nav-buttons>
...
I followed How to open an Ionic Modal from an Ionic Tab but it creates a tab that when pressed calls openModal(). I want to call openModal from a button on the title bar. I would also like to create only one controller for the modal that can be called from each tab. Is this possible and how would it be done?
Also note, I plan to show a list of objects in the modal. When an object is selected it will change the content of the tabs. Let me know if there might be an easier way to accomplish what I'm trying other than a modal.
Thanks! Mike
To further clarify
I’m new to Ionic and obviously I’m missing a key concept. In the below code I don’t know how to call methods that exist in another controller. For example, in the below code I don't know how to call the ModalController's openModal method from the FriendsCtrl's openModal method? Please see the below FriendsCtrl's openModal method.
Here's my code...
Tabs.html
<ion-tabs class="tabs-icon-top tabs-color-active-positive">
<ion-tab title="Home" icon-off="ion-ios-home-outline" icon-on="ion-ios-home" href="#/tab/home">
<ion-nav-view name="tab-home"></ion-nav-view>
</ion-tab>
<!-- Chats Tab -->
<ion-tab title="Friends" icon-off="ion-ios-people-outline" icon-on="ion-ios-people" href="#/tab/friends">
<ion-nav-view name="tab-friends"></ion-nav-view>
</ion-tab>
</ion-tabs>
Tabs-Friends.html
<ion-view view-title="Friends">
<ion-nav-buttons side="primary">
<button class="button button-icon icon ion-ios-minus-outline" ng-click="openModal()"></button>
</ion-nav-buttons>
<ion-content>
…
</ion-content>
</ion-view>
Modal-template.html
<ion-modal-view>
<ion-header-bar class="bar-positive">
<h1 class="title">Ionic Modal</h1>
<div class="buttons">
<button class="button button-clear" ng-click="closeModal()">Close</button>
</div>
</ion-header-bar>
</ion-modal-view>
Controllers.js
angular.module('starter.controllers', [])
.controller('HomePageCtrl', function ($scope, $window) {
$scope.openInBrowser = function (URL) {
$window.open(URL, '_system')
}
})
.controller('FriendsCtrl', function ($scope, $window, TodoList) {
…
// breakpoint verifies function called in debugger when the button is pressed.
// However, I don't know how to call the ModalController’s $scope.openModal from here?
$scope.openModal= function () {
$scope.modal.show();// to display ionic modal use it
}
})
.controller('ModalController', function ($scope, $timeout, $state, $ionicModal) {
//give same file name as your modal html file
$ionicModal.fromTemplateUrl('modal-template.html', {
scope: $scope
}).then(function (modal) {
$scope.modal = modal;
});
$scope.openModal= function () {
$scope.modal.show();// to display ionic modal use it
}
$scope.modal.show();// to display ionic modal use it
$scope.closeModal = function () {
$scope.modal.hide();// to hide ionic modal use it
}
});
Thanks for any help you can give.