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>

此程式碼將生成以下結果 −

廣告