簡體   English   中英

如何將數組傳遞給另一個組件?

[英]How can I pass an array to another component?

我有字符串數組 FidId,我需要傳遞這個數組以在另一個組件中使用

 public AddSelection(layer: VectorLayer, map:Map){
    this.vectorSource = layer;
      //start select
      var FidId:string[] = [];
      var FeatureFind = false;
      var select = new Select();

      map.addInteraction(select);
      var selectedFeature = select.getFeatures();

      this.dragBox = new DragBox({
        condition:(condition as any).platformModifierKeyOnly
      });
      map.addInteraction(this.dragBox);

      this.dragBox.on('boxend', () => {
        var extent = this.dragBox.getGeometry().getExtent();
        this.vectorSource.forEachFeatureIntersectingExtent(extent, function(feature) {
          FeatureFind = true;
          selectedFeature.push(feature);
          FidId.push(feature.get('id'));
        });

        if(FeatureFind)
        this.dialog.open(DialogData2Component);
        
      });
      this.dragBox.on('boxstart',()=>{
        selectedFeature.clear();
        FeatureFind = false;
        FidId = [];
      });
      //end select

  }

這是我需要它的地方

細節

我是 angular 的新手,所以請幫忙:)

我處理將任何 arguments 從我的孩子傳遞給父母的方式(在我看來這就是你需要的)

首先在我的子組件中定義我的 eventEmitter 並初始化我想傳遞給我的父級的數組

//child component.ts
array: any[] = [];
@Output() passArray = new Eventemitter

然后我寫一個 function 調用我的事件發射器並像這樣發出我的數組

// child component.ts
emitArray() {
  this.passArray.emit(array);
}

現在我將 go 放入我的父組件的 HTML 中,我的子組件位於此處,我可以通過將它放入這樣的括號中來收聽 eventEmitter

// parent component.html
<custom-component (passArray)="yourFunction($event)"><custom-component>

// parent component.ts
yourFunction(parameter) {
  do.something()
}

所以一旦 passArray.emit 被稱為我們的父組件,它在 html 中監聽它,執行“yourFunction”,其中包含從 $event 獲取的參數。 $event 是我們在子組件中傳遞的數據。 有關如何在 angular 中傳遞數據的更多詳細信息,您可以通讀https://angular.io/docs上的文檔。 他們很好地解釋了許多主題:)(比我好:D)

這是我的第一個答案之一,如果有點亂,那就太抱歉了:)我希望我能提供幫助;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM