将数据从模板传递到angular2中组件内的typescript文件

时间:2017-07-18 13:17:21

标签: angular angular2-template

有没有办法可以将一些数据从模板传递到同一个组件的.ts文件。方案是假设我有一个在模板中运行的循环,让我们说在i == 3我想要进行API调用然后在6假设相同的api调用,但使用其他一些过滤数据。所以最终我必须传递索引,以便当它达到特定值时,我可以相应地过滤数据。它只在一个组件内,所以数据绑定不是一个选项,我也想到了2路绑定,但我这里没有输入字段在这种情况下。请建议。

1 个答案:

答案 0 :(得分:0)

您可以点击这样做,就像我附加的示例一样,然后您可以在点击方法内部进行API调用并相应地传递数据

function getTest(){

  global $con;

  $get_pro = "select * from drinks";

    $run_pro = mysqli_query($con, $get_pro); 

    while($row=mysqli_fetch_array($run_pro)){

    $id = $row['id'];
    $title = $row['title'];
      $cat = $row['cat'];
      $image = $row['image'];
      $desc = $row['desc'];
      $qty = $row['qty'];
      $price =$row['price'];
      $status= $row['status'];


echo "<!--==========================\n"; 
echo "  Price Menu\n"; 
echo "============================-->\n"; 
echo "      <!-- Page Content -->\n"; 
echo "      <main class=\"entry-content\">\n"; 
echo "          <section class=\"page-section\">\n"; 
echo "              <div class=\"container\">\n"; 
echo "              \n"; 
echo "                  <div class=\"row\">\n"; 
echo "                      <div class=\"col-md-12\">\n"; 
echo "                          <div class=\"isotope-filters\" role=\"group\">\n"; 
echo "                              <div class=\"btn-group\">\n"; 

// the categories go here

echo "                              </div>\n"; 
echo "                          </div>\n"; 
echo "                      </div>\n"; 
echo "                  </div>\n"; 
echo "                  <br><br>\n"; 
echo "                  <div class=\"row isotope-wrapper isotope-beers-wrapper\">\n"; 
echo "                      <div class=\"isotope isotope-beers gutter\">\n"; 
echo "                          <div class=\"grid-item col-lg-3 col-md-3 col-sm-6 col-ms-6 col-xs-12 $cat\">\n"; 
echo "                              <div class=\"grid-wrapper\">\n"; 
echo "                                  <a href=\"javascript:void(0);\" data-remodal-target=\"bottle-$id\">\n"; 
echo "                                      <figure style=\"background-image: url('admin/images/drinks/$image')\">\n"; 
if ($row['status'] === 'Yes'){
echo "                                      <div class=\"offer\"></div>\n"; 
}
echo "                                          <figcaption class=\"grid-content\">\n"; 
echo "                                              <h5 class=\"grid-title\"><span>$title</span></h5>\n"; 
echo "                                          </figcaption>\n"; 
echo "                                      </figure>\n"; 
echo "                                  </a>\n"; 
echo "                              </div>\n"; 
echo "                          </div>\n"; 
echo "                      </div>\n"; 
echo "                  </div>\n"; 
echo "\n"; 
echo "              </div>\n"; 
echo "          </section>\n"; 
echo "      </main><!-- /.enry-content -->\n"; 
echo "\n"; 
echo "      <!-- Off-Page Content -->\n"; 
echo "      <!-- Mobile Navigation (Left Panel) -->\n"; 
echo "      <!-- Preloader -->\n"; 
echo "      <div class=\"introLoading\"></div>\n"; 
echo "      <!-- Back to Top -->\n"; 
echo "      <div>\n"; 
echo "          <a class=\"back-to-top fa fa-chevron-up\" href=\"javacript:void(0);\"></a>\n"; 
echo "      </div>\n"; 
echo "      <!-- Modal Content -->\n"; 
echo "      <div class=\"remodal-bg\">\n"; 
echo "          <!-- Item One -->\n"; 
echo "          <div class=\"remodal modal-beers\" data-remodal-id=\"bottle-$id\">\n"; 
echo "              <button data-remodal-action=\"close\" class=\"remodal-close\"></button>\n"; 
echo "              <div class=\"row\">\n"; 
echo "                  <div class=\"col-md-5 col-sm-12 col-xs-12\">\n"; 
echo "                      <div class=\"item-modal-image\">\n"; 
echo "                          <a class=\"image-lightbox\" href=\"admin/images/drinks/$image\"><img alt=\"\" src=\"admin/images/drinks/$image\" /></a>\n"; 
echo "                      </div>\n"; 
echo "                  </div>\n"; 
echo "                  <div class=\"col-md-7 col-sm-12 col-xs-12\">\n"; 
echo "                      <h3>$title</h3>\n"; 
echo "                      <p>$desc</p>\n"; 
echo "                      <table class=\"table\">\n"; 
echo "                          <tbody>\n"; 
echo "                              <tr>\n"; 
echo "                                  <td>Price:</td>\n"; 
echo "                                  <td>£$price</td>\n"; 
echo "                              </tr>\n"; 
echo "                              <tr>\n"; 
echo "                                  <td>Quantity:</td>\n"; 
echo "                                  <td>$qty</td>\n"; 
echo "                              </tr>\n"; 
echo "                          </tbody>\n"; 
echo "                      </table>\n"; 
echo "                  </div>\n"; 
echo "              </div>\n"; 
echo "          </div>\n"; 
echo "      </div>  \n"; 
echo "\n"; 
echo "    <!--==========================\n"; 
echo "  Price Menu end\n"; 
echo "============================-->\n";

}

}