亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用相同的函數將許多不同的值從子組件傳遞到父組件

使用相同的函數將許多不同的值從子組件傳遞到父組件

蠱毒傳說 2022-10-27 15:06:41
我有一個在父組件中使用的復選框組件。當值發生變化時,我使用 an@Output()讓父組件知道值已更新。我的代碼正在運行,但我在我的項目中多次使用復選框組件,我不想每次創建新函數來設置新的變量值時都創建它。這是我的代碼的鏈接我的問題是:我怎樣才能做得更好?為了防止onRoleChangeCheckboxX()每次我有一個新的復選框時都創建一個新功能。我的 stackblitz 只是一個例子,兩個額外的功能不會打擾我,但是當我必須創建 50 個復選框的情況下呢?
查看完整描述

3 回答

?
繁星點點滴滴

TA貢獻1803條經驗 獲得超3個贊

這是我認為您想要的堆棧閃電戰?您需要為您的復選框創建一個數組

  checkboxes = [

    {

      name: 'First checkbox',

      value: false

    },

    {

      name: 'Second checkbox',

      value: false

    },

  ]

使用 *ngFor 獲得一個通用 html,當您向數組添加另一個復選框時,您不需要編輯該 html


<app-checkbox *ngFor="let box of checkboxes; let i = index" 

              [checkboxName]="box.name" 

              [checkboxData]="box.value"  

              (toggle)="onRoleChangeCheckbox($event, i)"></app-checkbox>

然后您的onRoleChangeCheckbox()函數可以變得通用并使用索引更新您的數組


  onRoleChangeCheckbox(ev, index) {

    this.checkboxes[index].value = ev;

  }

這種方法應該可以減少很多重復的代碼,因為您只需要向數組添加新的復選框。


查看完整回答
反對 回復 2022-10-27
?
慕少森

TA貢獻2019條經驗 獲得超9個贊

可能這不是更好的方法,但它可能在某些情況下有效,在所有情況下都會有一個發射處理程序。


  import { Component, OnInit, Input, Output, EventEmitter } from "@angular/core";

 

    @Component({


  selector: "app-checkbox",

  templateUrl: "./checkbox.component.html",

  styleUrls: ["./checkbox.component.css"]

})

export class CheckboxComponent implements OnInit {

  @Input() checkboxName;

  @Input() checkboxData:any; 

  @Input() checkBoxLinkedProp:any; // added another property it is the name of the property in the parent component you are updating 

  

  @Output() toggle: EventEmitter<any> = new EventEmitter<any>(); // emit a object instead of bool


  constructor() {}


  ngOnInit() {}


  onToggle() {

    const checkedOption = this.checkboxData;

    this.toggle.emit({checked:checkedOption , checkBoxLinkedProp: this.checkBoxLinkedProp });

  }

}

app.component.html [checkBoxLinkedProp] = "'checkbox1Value'" - 我將道具名稱作為字符串傳遞給子復選框組件。并在切換時調用一個方法(toggle)="onRoleChangeCheckbox($event)"


this.toggle.emit將發射帶有我們傳遞的字符串道具的對象


<app-checkbox [checkboxName]='checkbox1' [checkboxData]='checkbox1Value' 

  [checkBoxLinkedProp] = "'checkbox1Value'"

 (toggle)="onRoleChangeCheckbox($event)"></app-checkbox>


<app-checkbox [checkboxName]='checkbox2' [checkboxData]='checkbox2Value'  (toggle)="onRoleChangeCheckbox($event)"

[checkBoxLinkedProp] = "'checkbox2Value'"

></app-checkbox>

app.component.ts onRoleChangeCheckbox({checkBoxLinkedProp , checked})這個方法將把我們作為字符串從發射事件傳遞過來的屬性名稱設置為類的狀態。


import { Component, VERSION } from '@angular/core';


@Component({

  selector: 'my-app',

  templateUrl: './app.component.html',

  styleUrls: [ './app.component.css' ]

})

export class AppComponent  {

  checkbox1 = 'First checkbox';

  checkbox1Value = false;


  checkbox2 = 'Second checkbox';

  checkbox2Value = false;


  onRoleChangeCheckbox({checkBoxLinkedProp , checked}) {

    this[checkBoxLinkedProp] = checked; // property name that passed to child , is received in this emit event and as this will be set

    console.log(this.checkbox1Value , checkBoxLinkedProp); // tested value for 1st checkbox

  }


}


查看完整回答
反對 回復 2022-10-27
?
胡說叔叔

TA貢獻1804條經驗 獲得超8個贊

我很難準確地說出你想要什么,但如果你使用 eventEmitter & Output,你可以用一個單一的發射語句發射一個復雜的 json obj,并向父級發送盡可能多的數據:


@Output() messageEvent = new EventEmitter<any>();


var obj = { check1: true, check2: false, check3: false }

this. messageEvent.emit(obj);


查看完整回答
反對 回復 2022-10-27
  • 3 回答
  • 0 關注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號