gpt4 book ai didi

Angular 2 : Pass value from attribute directive as a component variable

转载 作者:太空狗 更新时间:2023-10-29 17:43:53 24 4
gpt4 key购买 nike

所以我在这里有一个属性指令 appGetCurrency:

<md-select appGetCurrency [(ngModel)]="value" placeholder="Currency" name="currency">
<md-option *ngFor="let c of currencyList" [value]="c.code">{{c.dsc}}</md-option>
</md-select>

我希望 appGetCurrency 指令将一些值传递给 currencyList 以构建选项列表。

编辑

appGetCurrency 指令只是从服务中获取货币列表,然后我想将该列表传递给主机模板中的 currencyList 变量:

@Directive({ selector: '[appGetCurrency]' })

export class CurrencyDirective {
currencies;

constructor() {
// build the <md-options> based on 'currencies'
this.currencies = this.service.getCurrencies('asia');
}

}

最佳答案

您可以像在组件中一样使用EventEmitter

@Directive({ selector: '[appGetCurrency]' })

export class CurrencyDirective {
@Output() onCurrencyEvent = new EventEmitter();
currencies;

constructor() {
// build the <md-options> based on 'currencies'
this.currencies = this.service.getCurrencies('asia').subscribe((res)=>{
this.onCurrencyEvent.emit(res);
});
}

}

html:

<md-select appGetCurrency [(ngModel)]="value" placeholder="Currency" name="currency" (onCurrencyEvent)="currencyEventOnParent($event)">

父组件:

currencyEventOnParent(event){
console.log(event);
}

关于 Angular 2 : Pass value from attribute directive as a component variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43423718/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com