RIOT.JS - 循环



我们可以遍历 RIOT 中的基元数组或对象,并在此过程中创建/更新 html 元素。使用“each”构建即可实现这一过程。

  • 创建数组 − 创建对象数组。

this.cities = [
   { city : "Shanghai" , country:"China" , done: true },
   { city : "Seoul"    , country:"South Korea" },
   { city : "Moscow"   , country:"Russia"      }
];
  • 添加每个属性 − 现在使用“each”属性。

<ul>
   <li each = { cities } ></li>
</ul> 
  • 迭代对象数组 − 使用对象属性迭代数组。

<input type = "checkbox" checked = { done }> { city } - { country }

示例

以下是完整示例。

custom7Tag.tag

<custom7Tag>
   <style>
      ul {
         list-style-type: none;
      }
   </style>
   <ul>
      <li each = { cities } >
         <input type = "checkbox" checked = { done }> { city } - { country }
      </li>
   </ul>
   <script>
      this.cities = [
         { city : "Shanghai" , country:"China" , done: true },
         { city : "Seoul"    , country:"South Korea" },
         { city : "Moscow"   , country:"Russia"      }
      ]; 
   </script>
</custom7Tag>

custom7.htm

<html>
   <head>
      <script src = "https://cdnjs.cloudflare.com/ajax/libs/riot/3.13.2/riot+compiler.min.js"></script>
   </head>
   <body>
      <custom7Tag></custom6Tag>
      <script src = "custom7Tag.tag" type = "riot/tag"></script>
      <script>
         riot.mount("custom7Tag");
      </script>
   </body>
</html>

此代码将生成以下结果 −

广告