Osen ons.ready(...) and Ionic $ionicPlatform.ready(...)



  • I am looking at how to port bit of codes from an Ionic project to try out Onsen.

    angular.module('starter', ['ionic'])
      .controller('AppController', function($scope, $ionicPlatform){
        $ionicPlatform.ready(function() {
          $scope.function1 = function(){
            // do something useful
            // ... blah blah ...
            alert("Hello World!");
          }
    
          $scope.function1(); // Call function on ready.
        });
      });
    

    Now, for Onsen to do the same, is this the right way?

    ons.bootstrap()
      .controller('AppController', function($scope) {
    });
    
    ons.ready(function($scope) {
      $scope.function1 = function(){
        // do something useful
        // ... blah blah ...
        alert("Hello World!");
      }
    
      $scope.function1(); // Call function on ready.
    });
    

    I notice that ons.ready(...) is not nested within a controller unlike Ionic’s $ionicPlatform.ready(...). So can functions in ons.ready(...) access the controller $scope? Much apologies in advance, just an AngularJS newbie here.


  • Onsen UI

    @wetfeet no, you need to be inside the controller where you inject the $scope to be able to use it. The code you’re looking for in Onsen UI is pretty much the same:

    ons.bootstrap()
      .controller('AppController', function($scope){
        ons.ready(function() {
          $scope.function1 = function(){
            // do something useful
            // ... blah blah ...
            alert("Hello World!");
          }
    
          $scope.function1(); // Call function on ready.
        });
      });
    

Log in to reply