Hi,
I want to call controller inside same controller like nested controller, if condition is not satisfied .
.state('verifyData', {
cache: false,
url: '/verifyData',
controller: 'verifyDataController'
})
.controller('verifyDataController', ['ConnectivityMonitor','$scope', '$state', '$http', '$ionicPopup', '$ionicLoading', '$q', '$rootScope', function (ConnectivityMonitor, $scope, $state, $http, $ionicPopup, $ionicLoading, $q, $rootScope) {
document.addEventListener("deviceready", function() {
if(Connectivity._isOnline()) {
// do something(); this is working..
}
if(ConnectivityMonitor._isOffline()) {
$ionicPopup.alert({
title: "Error Status",
template: "checking whether internet is still working or no..",
onTap: function () {
alert("Inside offline status : " + ConnectivityMonitor._isOffline());
// i need to restart or reload the same controller... but it fails... it showing alert always
$state.go('/verifyData', {}, {reload: true});
}
});
}
}
});
.service('ConnectivityMonitor', function($rootScope, $cordovaNetwork){
this._isOnline = function () {
return $cordovaNetwork.isOnline();
};
this._isOffline = function () {
return $cordovaNetwork.isOffline();
};
this._networkType = function () {
return $cordovaNetwork.getNetwork();
};
});
Any help would be appreciated…
can you provide a codepen
How codepen will work with deviceready ?
Sorry @priteshpmehta I can’t get you
@reddy_dhitendra my code will not work on browser because online and offline event will only works with device… so how codepen will work with deviceready event ?
In my app i am checking whether the device has network or not… it can be wifi, 4g, etc… if there is no internet available then i am giving alert with $ionicpopup to reconnect to internet… i want that, once the user tap ionicpopup the same state should load again… to follow the same process till device comes with proper network…
I hope you get me…
Thank you.
okay,
try to use $route.reload() it will re-instantiates the controller.
so your controller will executed again.
If internet is available don’t call $route.reload()
I have tried this $route.reload()
but no luck… and in my case i need to force the app to re-instantiates the controller…whether internet is available or not… because on the start of the controller i am checking it…
any other solution ?
Don’t know this works or not, Just give a try
if there is no internet use one flag variable like isInternetAvailable make it false and call this._isOnline function from your controller if it returns false display alert again
module.controller(‘MyCtrl’, function($rootScope, $cordovaNetwork) {
document.addEventListener(“deviceready”, function () {
var type = $cordovaNetwork.getNetwork()
var isOnline = $cordovaNetwork.isOnline()
var isOffline = $cordovaNetwork.isOffline()
// listen for Online event
$rootScope.$on('$cordovaNetwork:online', function(event, networkState){
var onlineState = networkState;
})
// listen for Offline event
$rootScope.$on('$cordovaNetwork:offline', function(event, networkState){
var offlineState = networkState;
})
}, false);
});