简体   繁体   中英

how can I pass a value from parent component to directive in Angular 2?

If I have the following line of code in my parent component

<input datepicker type="text" (change)="update($event)"/>

How can I pass a value to the datepicker directive?

Define the input within the directive

datepicker.directive.ts

import {Input} from '@angular/core';
...
export class DatePickerDirective{
    @Input() config:any = {}; //set to default configuration
}

Now if you bind to the config property of this directive in the template, Angular will pass that value

parent.component.ts

dateConfig:any;

parent.component.html

<input datepicker type="text" [config]="dateConfig"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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