How to pass a model value to a new route in ember.js?

by elise_daugherty , in category: Javascript , 5 months ago

How to pass a model value to a new route in ember.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by samara , 5 months ago

@elise_daugherty 

To pass a model value to a new route in Ember.js, you can use the transitionToRoute method in the controller. Here's an example:

  1. In your controller file, add a method that will transition to the new route and pass the model value as a parameter:
1
2
3
4
5
actions: {
  goToNewRoute(modelValue) {
    this.transitionToRoute('newRoute', modelValue);
  }
}


  1. In your template file, you can trigger the action to pass the model value to the new route:
1
<button {{action 'goToNewRoute' model.value}}>Go to New Route</button>


  1. In your new route file, you can access the passed model value using the model hook:
1
2
3
4
5
6
import Route from '@ember/routing/route';
export default class NewRoute extends Route {
  model(params) {
    return params.modelValue;
  }
}


Now, when you click the button in your template, it will transition to the new route and pass the model value as a parameter. You can then access the model value in the new route by using the model hook.