代码之家  ›  专栏  ›  技术社区  ›  Char

角度2:父子零部件特性绑定

  •  1
  • Char  · 技术社区  · 8 年前

    如何对子组件进行属性绑定?我想让我的变量 high false or !this.high 通过其父组件,但事实是,子组件正在循环

    <button class="ui primary small button"(click)="clearVals()">Clear Selected</button>
    <app-product-list [dataSource]="data"></app-product-list>
    
    
    @ViewChild(ProductListComponent) prodList: ProductListComponent;
    clearVals() {
        this.selectedOutsourced = this.selectedPrice = 0;
        this.prodList.clear();
        this.selectedArray = [];
    }
    

    应用程序产品列表

    <div class="products-cards" *ngFor="let product of dataSource['docs']">
          <app-product-card [product]="product"(highlightEvent)="highlight($event)">
          </app-product-card>
    </div>
    
    
    @ViewChild(ProductCardComponent) prodCard: ProductCardComponent;
    

    应用产品卡

    <div class="list card ui" (click)="highlight()" [class.selected]="high"> </div>
    
    
    high : boolean = false;
    highlight(){
         this.high = !this.high;
    }
    

    2 回复  |  直到 8 年前
        1
  •  1
  •   Swoox    8 年前

    这是一个有趣的我刚刚注意到后,阅读这像5 thimes。

    你的div有一个ngFor。 您的孩子在该分区中,因此该孩子将被循环ofc。

    <div class="products-cards" *ngFor="let product of dataSource['docs']">
              <app-product-card [product]="product"(highlightEvent)="highlight($event)">
              </app-product-card>
        </div>
    

    应该是

        <div class="products-cards" *ngFor="let product of dataSource['docs']">
            </div>
    <app-product-card [product]="product"(highlightEvent)="highlight($event)">
                  </app-product-card>
    

    然后在你的孩子身上

    @Input()
      set product(product: any) {
        highlightF(product);
      }
    
    highlightf(product: any){
      this.hightlight.emit(product);
    }
    

    //Do something to set product.highlight
    
        2
  •  1
  •   Sunil Kumar    8 年前

    应用产品卡子组件 打字脚本

    import { Component, Input, Output, EventEmitter } from '@angular/core';
    
    @Input() product: any;
    
    @Output() highlightEvent= new EventEmitter();
    
    highlight(){
       this.highlightEvent.emit(somevalue);
    
    }