断言失败:#each 循环的值必须是数组余烬 js

Assertion Failed: The value that #each loops over must be an Array Embers js

本文关键字:数组 余烬 js 失败 #each 循环 断言      更新时间:2023-09-26

这是我的代码.

  <script type="text/x-handlebars">
        <h2>Welcome to Ember.js</h2>
        {{outlet}}
    </script>
    <script type="text/x-handlebars" data-template-name="index">      
            {{#each  model1}}
            <li>title</li>
            {{/each}}       
    </script>

和我的阵列控制器

App.IndexRoute = Ember.Route.extend({
});
App.IndexController = Ember.ArrayController.extend({
    model1: function () {
        return posts;
    } 
});

和我的杰森

posts = [{
        title: "Raja",
        body: "There are lots of à la carte software environments in this world." }, {
        title: "Broken Promises",
        body: "James Coglan wrote a lengthy article about Promises in node.js." }];

请告诉我是否可以从控制器调用 model1

是的,但它需要是一个属性,而不是一个函数

App.IndexController = Ember.ArrayController.extend({
    model1: function () {
        return posts;
    }.property()
});

但是,如果它是一个模型,那么从路由返回它并在控制器中使用该模型会更有意义

App.IndexRoute = Ember.Route.extend({
  model: function(){
    return posts;
  }
});
App.IndexController = Ember.ArrayController.extend();
<script type="text/x-handlebars" data-template-name="index">   
        <ul>   
        {{#each item in controller}}
          <li>{{item.title}}</li>
        {{/each}}       
        </ul>
</script>