Angular 2:将属性指令的值传递为组件变量

kyw*_*kyw 4 angular2-directives angular

所以我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>
Run Code Online (Sandbox Code Playgroud)

我希望该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'); 
  }

}
Run Code Online (Sandbox Code Playgroud)

eko*_*eko 10

您可以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);
    }); 
  }

}
Run Code Online (Sandbox Code Playgroud)

HTML:

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

父组件:

currencyEventOnParent(event){
  console.log(event);
}
Run Code Online (Sandbox Code Playgroud)