Search code examples
angularjsonsen-ui

Passing parameters in ons-sliding-menu


I am wondering if it is possible to open an ons-page as the main page in a ons-sliding-menu and passing it some parameters.

myMenu.setMainPage('somePage.html', [parameter1:'whatever'])

Then, once inside the page, recover them.

I know this can be done in the ons-navigator component. I didn't find any information about this issue.

In case this mechanism would not be available for sliding menu, can anyone recommend me a suitable one?

I am thinking in binding to the $rootScope the parameters I need:

$rootScope.parameter1 = 'whatever'

is this correct? Is there a better option?

Thanks for your help.


Solution

  • You don't need to use the root scope as long as the variable you're setting belongs to a scope that is a parent to that of the new page.

    So in your controller you can do something like

    $scope.params = {};
    

    And in the ngClick directive:

    params.parameter1 = something; myMenu.setMainPage('somePage.html');
    

    I made a simple example on Codepen: http://codepen.io/argelius/pen/GgQVRa

    However, a cleaner way would probably be to create a simple service to save the parameter. Then you inject the service in both controllers. If you do it this way you don't have to pollute the scope.