我的模板:
<main class="my_active">
<user myDirective="'false'">
</user>
</main>
我的指示
@Directive({
selector:'[myDirective]'
})
export class MyDerective {
@HostBinding('class.img')
isMyCirclePic:boolean;
@HostListener('mouseover')
onMouseEnter(){
console.log('VALUE BEFORE:', this.isMyCirclePic);
console.log('MAUS ENTER');
this.isMyCirclePic=true;
console.log('VALUE AFTER:', this.isMyCirclePic);
}
@HostListener('mouseleave')
onMouseLeave(){
console.log('VALUE BEFORE:', this.isMyCirclePic);
console.log('MAUS LEAVE');
this.isMyCirclePic=false;
console.log('VALUE AFTER:', this.isMyCirclePic);
}
}
更新
我已经使用@Input @HostBinding试用了解决方案。
<main class="my_active">
<user
myDerective
[myColor]="font_color">
</user>
</main>
我的myColor-Direktive:
@Directive({
selector:'[myColor]'
})
export class MyColor {
@HostBinding('style.color')
font_color:string;
@Input()
set myColor(color:string) {
this.font_color = color;
console.log('COLOR VALUE SET:', this.font_color);
}
get myColor():string{
console.log('COLOR VALUE GET:', this.font_color);
return this.font_color;
}
}
但它仍然无效。我可以看到,该值将由@ Input-param传递。我可以看到,将调用set-function。但是我无法看到,将会调用get函数,这是我所期待的。所以我认为没有变化。
答案 0 :(得分:1)
你可以使用@Input()@ ThemeBinding
@Directive({
selector:'[myDirective]'
})
export class MyDerective {
@Input() @HostBinding('class.img') isMyCirclePic:boolean;
@HostListener('mouseover')
onMouseEnter(){
console.log('VALUE BEFORE:', this.isMyCirclePic);
console.log('MAUS ENTER');
this.isMyCirclePic=true;
console.log('VALUE AFTER:', this.isMyCirclePic);
}
@HostListener('mouseleave')
onMouseLeave(){
console.log('VALUE BEFORE:', this.isMyCirclePic);
console.log('MAUS LEAVE');
this.isMyCirclePic=false;
console.log('VALUE AFTER:', this.isMyCirclePic);
}
}
将它绑定在tmeplate
<div myDirective [isMyCirclePic]="false">
myDirective
</div>