简体   繁体   English

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

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

So I have a attribute directive appGetCurrency in here: 所以我在这里有一个属性指令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>

I would like that the appGetCurrency directive to pass some values to the currencyList in order to build the list of options. 我希望appGetCurrency指令将一些值传递给currencyList以构建选项列表。

EDIT 编辑

The appGetCurrency directive just get a list of currencies from a service, then I would like to pass that list to the currencyList variable in the host template: appGetCurrency指令只是获取服务中的货币列表,然后我想将该列表传递给主机模板中的currencyList变量:

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

export class CurrencyDirective {
  currencies;

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

}

You can use an EventEmitter just like in a component 您可以像在组件中一样使用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: HTML:

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

Parent component: 父组件:

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

If you want to pass value to directive then you should try like this: 如果你想将值传递给指令那么你应该尝试这样:

This is my custom directive, and I am going to share two value from componen or HTML. 这是我的自定义指令,我将从componen或HTML共享两个值。

test.directive.ts: test.directive.ts:

import { Directive, ElementRef, Input, OnInit } from '@angular/core';

@Directive({
    selector: '[input-box]'
})

export class TestDirectives implements OnInit {
    @Input() name: string;
    @Input() value: string;
    constructor(private elementRef: ElementRef) {
    }
    ngOnInit() {
        console.log("input-box keys  : ", this.name, this.value);
    }
}

and now your directive has been created and you will have add this directive into your app.module.ts like below: 现在您的指令已经创建,您将把这个指令添加到app.module.ts如下所示:

app.module.ts: app.module.ts:

import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { TestDirectives } from '../directives/test.directive';


@NgModule({
  declarations: [
    AppComponent,
    TestDirectives
  ],
  imports: [],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

You will have to use your directive in your html and send data to the directive like in below code. 您必须在html中使用您的指令并将数据发送到指令,如下面的代码所示。

I am sending name and value to the test.directive.ts . 我正在向test.directive.ts发送namevalue

<div input-box [name]="'lightcyan'" [value]="'CYAN'"></div>

or 要么

<div input-box [name]="componentObject.name" [value]="componentObject.value'"></div>

Now see the console or use data in the directive accordingly. 现在查看控制台或相应地使用指令中的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM