3

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 to run my initialize code in Test.html ?

I tried to write them in 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 didn't work neither

please help advise. Thx

AlvaroAV
  • 10,335
  • 12
  • 60
  • 91
Lemon
  • 55
  • 3

1 Answers1

3

ons.ready() function is called when Onsen UI initialization is done. If the app is running under Cordova or PhoneGap, it will also wait for its initialization (ondeviceready event). That means that the code inside the function will be executed only when the app is fully loaded, and not when you change page.

To accomplish what you are trying to do, you can use a ons-navigator attribute called ons-postpush. It allows you to specify custom behavior when the "postpush" event is fired.

See more about ons-navigator attributes here

You can also call directly the function after the push page command has been sent by adding it inside ng-click, in this way:

<div class="grid9 red"
ng-click="myNavigatorHome.pushPage('Test.html',{ animation : 'slide' });
myFunction()">test</div>
Andi Pavllo
  • 2,506
  • 4
  • 18
  • 30
  • Sorry i'm coding webside first,maybe i know what problem.all of the javascript code only can write in the Top of Navigator page,like my index.html,because the pushPage function only reload the new page html body,so i put my subpage code to index.html ,and call it use "onTransitionEnd: function() {init();}",it's ok. but i have another question if i have too many Navigator level,how to coding them. – Lemon May 27 '15 at 08:18
  • @Lemon for other questions, please post a new question on stackoverflow. P.s. If my answer helped you, please consider to mark it as best answer, so it will be more valuable for users that will have the same problem in the future. – Andi Pavllo May 28 '15 at 02:22