Property is not recognized when using @Input in a directive

Asked

Viewed 31 times

1

Consider the following directive:

import {Directive, HostListener, HostBinding, Input} from '@angular/core';

@Directive({
    selector: '[myHighlight]'
})
export class HighlightDirective {

    @HostListener('mouseenter') onMouseOver() {
        this.backgroundColor = this.hightLightColor;
    }

    @HostListener('mouseleave') onMouseLeave() {
        this.backgroundColor = this.defaultColor;
    }

    @HostBinding('style.backgroundColor') backgroundColor:string;

    @Input() defaultColor:string = 'white';
    @Input() hightLightColor:string = 'yellow';

    constructor() {
    }

}

Note that 2 variables have been created defaultColor and hightLightColor using the developer @Input.

In the component’s HTML, I added the input Property:

<p myHighlight [defaultcolor]="'grey'" [hightlightcolor]="'red'">
    Texto highlight
</p>

However in the browser the following error occurs:

Uncaught Error: Template parse errors: 
Can't bind to 'defaultcolor' since it isn't a known property of 'p'.

<p myHighlight [ERROR ->][defaultcolor]="'grey' [hightlightcolor]="'red'">
    Texto highlight
</p>

Can't bind to 'hightlightcolor' since it isn't a known property of 'p'.

<p myHighlight [defaultcolor]="'grey'" [ERROR ->] [hightlightcolor]="'red'">
    Texto highlight
</p>

What is wrong in the code for that properties defaultcolor and hightlightcolor are not recognised?

  • I changed the name of the variables and it worked. I put the old name back and it worked anyway. There is some cache when using the ng server in the CLI?

  • Put your solution to answer, Filipe. You can help someone with the same problem.

  • @Pabloalmeida is hardly a solution. In fact I did not understand yet what happened, I used the comments to add the information because it is not a concrete answer.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.