0

A route /numbers has the JavaScript:

// app/routes/numbers.js
// ...
numbers: [1,2,3,4,5],
// ...

and in the route's template:

<!-- app/templates/numbers.hbs -->
{{#each numbers as |n|}}
  <p>{{n}}</p>
{{/each}}

When I view the /numbers route, nothing is rendered inside the {{each}} helper. Why not?

1
  • Note that any variables you want be accessed in templates, should be defined in controllers Commented Jul 6, 2016 at 6:38

1 Answer 1

1

You cannot access properties that you set in the route in the template. If you want to access the numbers array in the template, then you will need to set it in the controller using setupController.

The "Ember way" to pass data to templates is to get the data in the route's model hook, then set it on the controller in setupController

In your route, do this:

model: function(params, transition) {
  return this.get('numbers');
},

setupController: function(controller, model) {
  controller.set('model', model);
}

then in your template, instead of accessing the numbers array, use model

{{#each model as |n|}}
  <p>{{n}}</p>
{{/each}}

for more information read this: https://guides.emberjs.com/v2.6.0/routing/specifying-a-routes-model/

Sign up to request clarification or add additional context in comments.

1 Comment

setupController isn't necessary. But if you want setup another property to the controller. you use it. controller.set("number",model);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.