2

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

2 Answers2

1

In app.module.ts I had

 bootstrap: [AppComponent, LikeComponent]

and changed it to:

 bootstrap: [AppComponent]

now the properties bind as expected.

0

You should just have

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

and

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

DEMO STACKBLITZ

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • That is not working for me either. I saw some posts that you should avoid camel casing in HTML but that is what I started with. – Megan Cunningham May 21 '18 at 16:00
  • I attached a screenshot of the console output from developer tools to the end. I have not used stackblitz but will work on that later. – Megan Cunningham May 21 '18 at 16:08
  • CHECK THE DEMO https://stackblitz.com/edit/angular-parent-child-interaction-qpexck?file=app%2Flike.component.ts – Sajeetharan May 21 '18 at 16:26
  • @Vikas i have just added the component interaction part, logic OP has to deat with – Sajeetharan May 21 '18 at 16:32
  • It was because I had 'LikeComponent' listed in the bootstrap piece with 'AppComponent'. Thanks for the demo! I will use stackblitz in the future. So changing bootstrap: [AppComponent, LikeComponent] -> bootstrap: [AppComponent] fixed the issue. – Megan Cunningham May 21 '18 at 16:34
  • It won't let me mark it yet. I do not have enough points as I just created an account. Once I get more I will mark. I posted the fix but will upvote this thread. – Megan Cunningham May 21 '18 at 16:36