Damian Hetman Damian Hetman - 21 days ago 11
AngularJS Question

Ionic/AngularJS - state.go without controller

I use Ionic for builing apps. I have one problem that I don't know how to solve. I want to redirect user to another tab. I read about state.go() but these probably won't work here. It's not in controller. User needs to be redirect to another tab after he receive notification and clicks "Show". This is my code. Can you help me?



angular.module('starter', ['ionic', 'starter.controllers', 'starter.services'])

.run(function($ionicPlatform, $ionicPopup) {

$ionicPlatform.ready(function() {

if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
}

if (window.StatusBar) {
StatusBar.styleDefault();
}

FCMPlugin.getToken(
function(token){
console.log("Użytkownik zarejestrowany");
},
function(err){
console.log('Nie udało się połączyć z usługą powiadamiania');
}
)

FCMPlugin.onNotification(
function(data){
if (data.wasTapped){
// I need that code here
} else {

var confirmPopup = $ionicPopup.confirm({
title: "Notification",
template: 'Want to read?',
cancelText: 'I want to read',
okText: 'Nope'
});

confirmPopup.then(function(res) {
if (res) {
// I need that code here
}
});

}
},
function(msg){
console.log("Ok");
},
function(err){
console.log("Error");
}
);

});
})




Answer

You can use the $state of ui-router even in run block.

Just add the dependency $state as the dependency of run block.

.run(function($ionicPlatform, $ionicPopup, $state) {

    // code goes here

    $state.go('zglos')
}