How to initialize a subpage when I pushPage to it
in my index.html
<div class="grid9 red" onclick="myNavigatorHome.pushPage('Test.html', { animation : 'slide' } )">
test
</div>
When I click it will pushPage to Test.html, but:
- How do I run my init code in Test.html?
I tried to write them down to Test.html
ons.ready(function() {//do some thing});
but it didn't work
I also tried to write in Test.html
document.addEventListener("pageinit", function(e) {//do some thing}
but it doesn't work either
please help advise. thank
source to share
ons.ready()
the function is called when the initialization of the Onsen UI is initialized. If the application is running Cordova or PhoneGap, it also waits for it to initialize (event ondeviceready
). This means that the code inside the function will only be executed when the application is fully loaded, not when the page changes.
To accomplish what you are trying to do, you can use an attribute ons-navigator
called ons-postpush
. It allows you to specify custom behavior when the "postpush" event is fired.
More about ons-navigator
attributes here
You can also call the function directly after sending the push page command by adding it inside ng-click
, like this:
<div class="grid9 red"
ng-click="myNavigatorHome.pushPage('Test.html',{ animation : 'slide' });
myFunction()">test</div>
source to share