I have an Ionic app that makes use of a sidebar. In this sidebar there is the functionality to refresh your data which goes off to the servers and returns a new JSON object that I save to the local storage.
The problem that I'm facing here is that when you open up the sidemenu and hit the 'Refresh My Data' menu item, the data does indeed get returned and saved locally successfully but I also need to refresh or re-run the controller of whichever view/state I'm on, so that the current view can make use of the new data that is in storage.
My index.html:
<ion-side-menu side="left" ng-controller="sidebarController"> <ion-header-bar class="bar-positive"> <h1 class="title">Menu</h1> </ion-header-bar> <div class="list has-header"> <div class="item item-icon-left" ng-click="refreshData()"><i class="icon ion-refresh"></i> Refresh My Data</div> </div> </ion-side-menu> <ion-side-menu-content> <ion-nav-view></ion-nav-view> </ion-side-menu-content> </ion-side-menus> As you can see the sidebar has it's own dedicated controller called sidebarController which you can see here:
app.controller('sidebarController', ['$scope', '$ionicSideMenuDelegate', '$storage', '$registerService', '$ionicLoading', '$ionicPopup', function($scope, $ionicSideMenuDelegate, $storage, $registerService, $ionicLoading, $ionicPopup) { $scope.refreshData = function () { // Display loading modal $ionicLoading.show({ template: 'Syncing with server...' }); // Call the getMemberDetails service var memberNo = $storage.get('memberNo', ''); $registerService.getMemberDetails(memberNo). success(function(data, status, headers, config) { // Store details to local storage $storage.setObject('member', data.member); // Close loading modal and sidebar $ionicLoading.hide(); closeSidebar(); // Display success message var alertPopup = $ionicPopup.alert({ title: 'Success', template: 'Your data hsa been successfully sycned.' }); }). error(function(data, status, headers, config) { $ionicLoading.hide(); var alertPopup = $ionicPopup.alert({ title: 'Oops', template: 'An error occured while trying to sync with our servers. Please make sure you have a data connection.' }); // Close the sidebar closeSidebar(); }); } function closeSidebar () { if ($ionicSideMenuDelegate.isOpenLeft()) { $ionicSideMenuDelegate.toggleLeft(); } } } ]); So basically after refreshData () has run, I need to find out which state or view I'm currently in, and then re-run the controller for that view. Is there a simple way of doing this?
$broadcastor$emit, or reload the view entirely.