第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

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

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

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

3 回答

?
繁星點(diǎn)點(diǎn)滴滴

TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超3個(gè)贊

這是我認(rèn)為您想要的堆棧閃電戰(zhàn)?您需要為您的復(fù)選框創(chuàng)建一個(gè)數(shù)組

  checkboxes = [

    {

      name: 'First checkbox',

      value: false

    },

    {

      name: 'Second checkbox',

      value: false

    },

  ]

使用 *ngFor 獲得一個(gè)通用 html,當(dāng)您向數(shù)組添加另一個(gè)復(fù)選框時(shí),您不需要編輯該 html


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

              [checkboxName]="box.name" 

              [checkboxData]="box.value"  

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

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


  onRoleChangeCheckbox(ev, index) {

    this.checkboxes[index].value = ev;

  }

這種方法應(yīng)該可以減少很多重復(fù)的代碼,因?yàn)槟恍枰驍?shù)組添加新的復(fù)選框。


查看完整回答
反對(duì) 回復(fù) 2022-10-27
?
慕少森

TA貢獻(xiàn)2019條經(jīng)驗(yàn) 獲得超9個(gè)贊

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


  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'" - 我將道具名稱(chēng)作為字符串傳遞給子復(fù)選框組件。并在切換時(shí)調(diào)用一個(gè)方法(toggle)="onRoleChangeCheckbox($event)"


this.toggle.emit將發(fā)射帶有我們傳遞的字符串道具的對(duì)象


<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})這個(gè)方法將把我們作為字符串從發(fā)射事件傳遞過(guò)來(lái)的屬性名稱(chēng)設(shè)置為類(lèi)的狀態(tài)。


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

  }


}


查看完整回答
反對(duì) 回復(fù) 2022-10-27
?
胡說(shuō)叔叔

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超8個(gè)贊

我很難準(zhǔn)確地說(shuō)出你想要什么,但如果你使用 eventEmitter & Output,你可以用一個(gè)單一的發(fā)射語(yǔ)句發(fā)射一個(gè)復(fù)雜的 json obj,并向父級(jí)發(fā)送盡可能多的數(shù)據(jù):


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


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

this. messageEvent.emit(obj);


查看完整回答
反對(duì) 回復(fù) 2022-10-27
  • 3 回答
  • 0 關(guān)注
  • 112 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)