[英]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). 我试图向我的自定义组件编写一个简单的指令,该指令基于将值传递给ther组件(也传递给ngModel)的值(来自ngModel)。
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. 但是,而不是我的输入组件传递“ dateOfBirth”字符串。 I hope that's a silly, schollboy mistake or typo error, but I can't handle it.
我希望这是一个愚蠢的,schollboy错误或错别字错误,但我无法解决。 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}}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.