尝试输入属性绑定时,我将属性设置为undefined。 带输入的组件: 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>
我在Like组件中声明了输入属性,然后将它们绑定在app组件中 - 但似乎没有传递值: Developer Tools Console
答案 0 :(得分:0)
你应该有
<like
[likescount]="likesCount"
[isliked]="isSelected"
></like>
和
@Input() isLiked: boolean;
@Input() likesCount: number;
<强> DEMO STACKBLITZ
强>
答案 1 :(得分:0)
在app.module.ts我有
bootstrap: [AppComponent, LikeComponent]
并将其更改为:
bootstrap: [AppComponent]
现在属性按预期绑定。