1

I'm gonna pass a value into my custom component but it shows me an error in console

Can't bind to 'isFavorite' since it isn't a known native property

here is my code:

favorite.component.ts

import {Component,Input} from 'angular2/core'
@Component ({
    selector:'favorite',
    template:`
    <i class="glyphicon"
    [class.glyphicon-star-empty]="!isFavorite"
    [class.glyphicon-star]="isFavorite"
    (click)="onClick()">
    </i>
    `
})
export class FavoriteComponent{
    @Input() isFavorite = false;
    onClick(){
        this.isFavorite = !this.isFavorite;
    }
}

app.component.ts

import {Component} from 'angular2/core'
import {FavoriteComponent} from './favorite.component'

@Component({
    selector: 'my-app',
    template: `
    <favorite [isFavorite]="true"></favorite>
    `,
   directives: [FavoriteComponent]
})
export class AppComponent {}

UPDATE: the above code is better way to achieve this functionality and you can fix the problem by passing the name to @input() -> @Input('isFavorite') isFavorite = false;


different way but the same result

i also tried below code but it shows me the same error!

favorite.component.ts

import {Component,Input} from 'angular2/core'
@Component ({
    selector:'favorite',
    template:`
    <i class="glyphicon"
    [class.glyphicon-star-empty]="!isFavorite"
    [class.glyphicon-star]="isFavorite"
    (click)="onClick()">
    </i>
    `,
    inputs: ['isFavorite:is-favorite']
})
export class FavoriteComponent{
    @Input('is-favorite') isFavorite = false;
    onClick(){
        this.isFavorite = !this.isFavorite;
    }
}

app.component.ts

import {Component} from 'angular2/core'
import {FavoriteComponent} from './favorite.component'

@Component({
    selector: 'my-app',
    template: `
    <favorite is-favorite="true"></favorite>
    `,
   directives: [FavoriteComponent]
})
export class AppComponent {}

what's wrong in my code?

8
  • Does your component is applied without the input? Commented Jun 13, 2016 at 11:14
  • Have you tried without inputs: ['isFavorite:is-favorite']? Commented Jun 13, 2016 at 11:15
  • What version of angular2 are you using? Commented Jun 13, 2016 at 11:19
  • @Günter Zöchbauer yes, you can see in my question Commented Jun 13, 2016 at 11:22
  • 1
    You need to have quite an old version (alpha.x) for this not to work. I use Angular since the first beta and have never seen an issue with this. Do you still have the inputs: [...] in the @Component(...) decorator? Commented Jun 14, 2016 at 6:55

1 Answer 1

1

Only use inputs: [...] OR @Input(...) but not both at the same time:

import {Component,Input} from 'angular2/core'
@Component ({
    selector:'favorite',
    template:`
    <i class="glyphicon"
    [class.glyphicon-star-empty]="!isFavorite"
    [class.glyphicon-star]="isFavorite"
    (click)="onClick()">
    </i>
    `,
    // inputs: ['isFavorite:is-favorite']
})
export class FavoriteComponent{
    @Input('is-favorite') isFavorite = false;
    onClick(){
        this.isFavorite = !this.isFavorite;
    }
}

The error messages comes from

<favorite [isFavorite]="true"></favorite>

If you use

inputs: ['isFavorite:is-favorite']

or

@Input('is-favorite') isFavorite = false;

it means the property is named is-favorite but the value is assigned to the class field isFavorite.

If you want to use it like

<favorite [isFavorite]="true"></favorite>

define it like

inputs: ['isFavorite']

or

@Input() isFavorite = false;
Sign up to request clarification or add additional context in comments.

7 Comments

isFavorite is always false (uses default class field) but i want to make it true
How do you want to use it? is-favorite="..." or isFavorite="..."?
If you want to assign the value true instead of the string 'true', then use [isFavorite]="true"
i'm using @Input() isFavorite = false; and <favorite [isFavorite]="true"></favorite> but it doesn't work!
Did you check my plunker? I think this is what you want.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.