Skip to content

Instantly share code, notes, and snippets.

@linxlad
Created June 26, 2016 00:55
Show Gist options
  • Save linxlad/fe5e07f0987d3a2a12764e786c498b68 to your computer and use it in GitHub Desktop.
Save linxlad/fe5e07f0987d3a2a12764e786c498b68 to your computer and use it in GitHub Desktop.
Angular 2 Twitter heart like icon component.
import {Component, Input, Output, EventEmitter} from 'angular2/core'
/**
* <like [totalLikes]="post.totalLikes" [iLike]="post.iLike" (change)="oniLikeChange($event)"></like>
*/
@Component({
selector: 'like',
template: `
<li
class="glyphicon glyphicon-heart"
[class.highlighted]="iLike"
(click)="onClick()"
>
</li>
`,
styles: [`
.glyphicon-heart {
color: #ccc;
font-size: 64px;
cursor: pointer;
}
.highlighted {
color: deeppink;
}
`]
})
export class LikeComponent {
@Input() totalLikes = 0;
@Input() iLike = false;
@Output() change = new EventEmitter();
onClick() {
this.iLike = !this.iLike;
this.totalLikes += this.iLike ? 1 : -1;
this.change.emit({newValue: this.iLike, newTotal: this.totalLikes});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment