版本2.4.1上的ember.js循环失败

时间:2016-03-12 12:37:06

标签: javascript ember.js

我有关于ember 1.10的tutorialpoint的以下代码。它工作正常。

<!DOCTYPE html>
<html>
   <head>
      <title>Emberjs displaying list of items</title>
      <!-- CDN's-->
      <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.1/handlebars.min.js"></script>
      <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.10.0/ember.min.js"></script>
      <script src="http://builds.emberjs.com/tags/v1.10.0-beta.3/ember-template-compiler.js"></script>
      <script src="http://builds.emberjs.com/release/ember.debug.js"></script>
      <script src="http://builds.emberjs.com/beta/ember-data.js"></script>
   </head>
   <body>
      <script type="text/x-handlebars">
         <ul>
           <!-- Getting each value of the myArray -->
            {{#each value in App.myArray}}
               <!-- diplaying the values -->
               <li>{{value}}</li>
            {{/each}}
         </ul>
      </script>

      <script type="text/javascript">
         var App = Ember.Application.create()
         //creating the array
         App.myArray = ['Mona','Manu','Mack']
      </script>
   </body>
</html>

但是如果我从

更改了版本
  <script src="https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.10.0/ember.min.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.4.1/ember.min.js"></script>

然后代码停止工作,没有错误。我的第一个问题是如何才能使其在2.4下工作?

我的第二个问题是,即使在版本1.1下,如果我使用id =&#34; {{value}}&#34;它不会起作用。如何将{{}}分配给DOM元素id,像这样?

<!DOCTYPE html>
<html>
   <head>
      <title>Emberjs displaying list of items</title>
      <!-- CDN's-->
      <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.5/handlebars.min.js"></script>
      <script src="https://code.jquery.com/jquery-2.2.1.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.10.0/ember.min.js"></script>
      <script src="http://builds.emberjs.com/tags/v1.10.0-beta.3/ember-template-compiler.js"></script>
      <script src="http://builds.emberjs.com/release/ember.debug.js"></script>
      <script src="http://builds.emberjs.com/beta/ember-data.js"></script>
   </head>
   <body>
      <script type="text/x-handlebars">
         <ul>
           <!-- Getting each value of the myArray -->
            {{#each value in App.myArray}}
               <!-- diplaying the values -->
               <li id="{{value}}">{{value}}</li>
            {{/each}}
         </ul>
      </script>

      <script type="text/javascript">
         var App = Ember.Application.create()
         //creating the array
         App.myArray = ['Mona','Manu','Mack']
      </script>
   </body>
</html>

更新: 我改为新语法如下,但它仍然无法工作。

   <body>
      <script type="text/x-handlebars">         
        <ul>
          {{#each App.people as |person|}}
            <li>Hello, {{person}}!</li>
          {{/each}}
        </ul> 
      </script>

      <script type="text/javascript">
         var App = Ember.Application.create()
         //creating the array
         App.people = ['Mona','Manu','Mack']
      </script>
   </body>  

1 个答案:

答案 0 :(得分:3)

Ember 2.x不会在模板中进行全局查找,因此像SomeConst.someProperty这样的代码正在寻找名为SomeConst local 属性,而不是全局变量。

它只会对其上下文进行本地查找(如果模板由路由器呈现,则为控制器实例;如果模板由组件呈现,则更常见的是组件的实例。

以下是您正在尝试的实例:http://jsbin.com/racacitiko/1/edit?html