Javascript 将输入值从component.html传递到component.ts

Javascript 将输入值从component.html传递到component.ts,javascript,angular,Javascript,Angular,我是angular的新手,我正试图通过点击按钮将产品添加到我的购物车中 产品是动态生成的 这是我的component.html <div class="row"> <div class="col-lg-4 col-md-4 col-sm-12 col-xs-12"> <a href="#" class="card clickable"> <div class="card-im

我是angular的新手,我正试图通过点击按钮将产品添加到我的购物车中 产品是动态生成的 这是我的component.html

    <div class="row">
        <div class="col-lg-4 col-md-4 col-sm-12 col-xs-12">
            <a href="#" class="card clickable">
                <div class="card-img">
                    <img src="">
                </div>
                <div class="card-block">
                        <h5>product1</h5>
                        <h5>aweeee112ddddd</h5>
                        <h5>1</h5>
                        <input type="hidden" name=""  value="aweeee112ddddd" id="" #productid>
                    <p class="card-text">

                        <button class="btn btn-outline" (click)="addtocart(this.productid)">Addtocart</button>
                    </p>
                </div>
            </a>
        </div>

        <div class="col-lg-4 col-md-4 col-sm-12 col-xs-12">
                <a href="#" class="card clickable">
                    <div class="card-img">
                        <img src="">
                    </div>
                    <div class="card-block">
                        <p class="card-text">
                                <input type="hidden" name=""  value="vvvv112ddddd" id="" #productid>
 <input type="hidden" name=""  value="product1" id="" #productname>
                            <button class="btn btn-outline" (click)="addtocart(this.productid)">Addtocart</button>
                        </p>
                    </div>
                </a>
            </div>

            <div class="col-lg-4 col-md-4 col-sm-12 col-xs-12">
                    <a href="#" class="card clickable">
                        <div class="card-img">
                            <img src="">
                        </div>
                        <div class="card-block">
<input type="hidden" name=""  value="vppp112ddddd" id="" #productid>
 <input type="hidden" name=""  value="product2" id="" #productname>
                            <p class="card-text">
                                <button class="btn btn-outline" (click)="addtocart()">Addtocart</button>
                            </p>
                        </div>
                    </a>
                </div>
    </div>

我想将我的隐藏字段值传递给component.ts,产品将动态显示,从数据库中获得的任何帮助都将非常感谢

尽管您可以使用ngModel,但在我看来,最好使用表单控件。至少,您将获得rxjs的所有功能(通过valueChanges)。 要执行此操作,您应该:

  • ReactiveFormsModule
    导入主机模块
  • formCotrol
    指令添加到
    input
    -例如。
  • 在组件中定义表单控件-
    public myControl=new FormControl()
  • 然后将其作为

    use[(ngModel)]指令使用
    export class Products  {
    
        addtocart(item1,item2,item3){
            alert(item.value);
            //alert("item added");
             this.cartservice.additems(item1,item2,item3);
            return false;
        }
        constructor(public cartservice:CartService){
    
        }}