Kornel B.
Kornel B.

Reputation: 17

onsen-ui with phonegap: push page on app ready

I check on device.ready the local storage whether the user is already logged in or not. Then I need to navigate either to "home.html" or "login.html" but I always get the reference error "Uncaught ReferenceError: ons is not defined".

ons.navigator.pushPage('home.html');

I tried also with window.onload or $(document).ready but I always get the same error. Button clicks works fine.

Thanks in advice, Kornel

Upvotes: 0

Views: 3681

Answers (3)

Navin S
Navin S

Reputation: 5

Try the below code instead:

menu.setMainPage('home.html', {closeMenu: true});

Upvotes: 0

Jesus
Jesus

Reputation: 437

ons.ready liste for cordova deviceready event and onsen loaded

ons.ready(function(){

   myNavigator.pushPage('home.html');

});

Upvotes: 3

KNaito
KNaito

Reputation: 3962

There are two problems. One is a scope of Navigator and the other is timing. Unfortunately the current Onsen-UI (ver 1.0.4) does not have a trigger the page is loaded. You must wait until the page of navigator has been loaded.

For example,

    document.addEventListener('deviceready',onDeviceReady,false);

    function onDeviceReady() {
        checkScope();
    }

    function checkScope() {
        var element = document.querySelector( ".navigator-container");
        var scope = angular.element( element ).scope();
        if (scope && scope.getCurrentNavigatorItem && scope.getCurrentNavigatorItem() ) {
            scope.pushPage("page2.html");
        } else {
            setTimeout( checkScope , 100 );
        }

    }

Upvotes: 1

Related Questions