简体   繁体   中英

Passing component to directive by input

I totally don't know what's going on.

I was trying write a simple directive to my custom component, which based on value (from ngModel) passing value to ther component (to ngModel too).

It's looks like that:

<form-text
    required
    birthDateExtracter="dateOfBirth"
    name="id"
    [(ngModel)]="model.idNumber"></form-text>

<form-datepicker #dateOfBirth
    name="Birth Date"
    [(ngModel)]="model.birthDate"></form-datepicker>

and my directive looks like:

@Directive({
  selector: '[ngModel][birthDateExtracter]'
})
export class BirthDateExtracterDirective {

  _component: any;

  @Input('birthDateExtracter')
  set birthDate(value: any) {
    this._component = value;
    console.log(value); //it's not working
  }

  constructor(private model: NgModel) {
  }

  @HostListener('ngModelChange', ['$event'])
  onModelChange(event) {
    console.log(event); //it works fine
  }
}

But instead of component to my Input was passing "dateOfBirth" string. I hope that's a silly, schollboy mistake or typo error, but I can't handle it. Or maybe I should make it in another way. Is there a mistake in this way?

Seems you forgot square brackets. I guess it should be:

[birthDateExtracter]="dateOfBirth"

or another way is using interpolation

birthDateExtracter="{{dateOfBirth}}"

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