Megan Cunningham
Megan Cunningham

Reputation: 31

Why would my Input Property Binding not work? - Angular

I am getting the property as undefined when attempting input property binding. Component with inputs: like.component.ts `

import { Component, OnInit, Input } from '@angular/core';

@Component({
  selector: 'like',
  templateUrl: './like.component.html',
  styleUrls: ['./like.component.css']
})
export class LikeComponent{
  @Input('is-liked') isLiked: boolean;
  @Input('likes-count') likesCount: number;


  onClick(){
    console.log("clicked");
    console.log(this.likesCount);
    console.log(this.isLiked);
    this.likesCount += (this.isLiked) ? -1 : 1;
    this.isLiked = !this.isLiked;

  }

}

` like.component.html

<p>
  likes work
</p>
<span class="glyphicon glyphicon-heart"
      [class.highlighted]="isLiked"
      (click)=onClick()></span>
<span>{{ likesCount }}</span>

app.component.ts

import { Component } from '@angular/core';
import { LikeComponent } from './like/like.component';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

    isSelected = true;
    likesCount = 5;


}

app.component.html

<like 
[likes-count]="likesCount"
[is-liked]="isSelected"
></like>

I declare the input properties in the Like Component and then bind them in the app component - but it seems the values are not being passed: Developer Tools Console

Upvotes: 2

Views: 1875

Answers (2)

Megan Cunningham
Megan Cunningham

Reputation: 31

In app.module.ts I had

 bootstrap: [AppComponent, LikeComponent]

and changed it to:

 bootstrap: [AppComponent]

now the properties bind as expected.

Upvotes: 1

Sajeetharan
Sajeetharan

Reputation: 222522

You should just have

<like 
[likescount]="likesCount"
[isliked]="isSelected"
></like>

and

 @Input() isLiked: boolean;
 @Input() likesCount: number;

DEMO STACKBLITZ

Upvotes: 0

Related Questions