angularjs - $state.go to a named view -
i struggling ui-router, ionic , named views.
in previous angular projects checked in main app.js file if logged in, if not redirected them login state using $state.go();
an example of this:
angular.module() ... .run(function() { $rootscope.$on('$statechangestart', function() { if(loginservice.isloggedin() !== true) { $state.go('login'); } } }); now simplified version of i'm using shows mean (i hope).
this works great in other projects, trying out ionic , there use named views. when app loads redirects me correctly login page. when hits (tab) button. redirects correct url (/login) view not loaded tab button refers named view: e.g:
the tabs:
<!-- dashboard tab --> <ion-tab title="user" icon-off="ion-ios-person" icon-on="ion-ios-person" href="#/tab/dash"> <ion-nav-view name="tab-dash"></ion-nav-view> </ion-tab> <!-- chats tab --> <ion-tab title="chats" icon-off="ion-ios-chatboxes-outline" icon-on="ion-ios-chatboxes" href="#/tab/chats"> <ion-nav-view name="tab-chats"></ion-nav-view> </ion-tab> <!-- account tab --> <ion-tab title="account" icon-off="ion-ios-gear-outline" icon-on="ion-ios-gear" href="#/tab/account"> <ion-nav-view name="tab-account"></ion-nav-view> </ion-tab> the states defined follows:
$stateprovider // setup abstract state tabs directive .state('tab', { url: "/tab", abstract: true, templateurl: "templates/tabs.html" }) // each tab has own nav history stack: .state('tab.dash', { url: '/dash', views: { 'tab-dash': { templateurl: 'templates/tab-dash.html', controller: 'dashctrl' } } }) can use $state.go not redirect me state use specific named view?
i've been looking around few hours can't find descent answer.
you saying want prevent user see view without being logged in, if not should redirected /login.you can try loginservice checking if route requires authentication or check authenticated.
angular.module("myapp") .run(function ($rootscope, $state, loginservice) { $rootscope.$on("$statechangestart", function(event, tostate, toparams, fromstate, fromparams){ if (tostate.authenticate && !loginservice.isauthenticated()){ // user isn’t authenticated $state.transitionto("login"); event.preventdefault(); } }); }); then can fix routes using authenticate:true
myapp.config(function($stateprovider, $urlrouterprovider){ $stateprovider .state("tab.dash", { url: "/dash", views: { 'tab-dash': { templateurl: 'templates/tab-dash.html', controller: 'dashctrl' }, authenticate: true }) .state("login", { url: "/login", templateurl: "templates/login.html", controller: "loginctrl", authenticate: false }); // send login if url not found $urlrouterprovider.otherwise("/login"); });
Comments
Post a Comment