Skip to content

Instantly share code, notes, and snippets.

@navix
Created April 12, 2017 10:20
Show Gist options
  • Select an option

  • Save navix/c9a12ce35804bcf24ff8c8e3ff786938 to your computer and use it in GitHub Desktop.

Select an option

Save navix/c9a12ce35804bcf24ff8c8e3ff786938 to your computer and use it in GitHub Desktop.
Angular: input/output decorators
<div *ngFor="let person of persons">
<app-person [data]="person" (upVote)="upVote(person, $event)"></app-person>
</div>
import { Component, OnInit } from '@angular/core';
import { DataService } from './data.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'app works!!!';
persons;
constructor(private dataService: DataService) {
}
ngOnInit() {
this.persons = this.dataService.getPersons();
}
upVote(person, inc) {
person.points += inc;
}
}
Person! {{ data.name }}: {{ data.points }}
<button (click)="upVoteClick(1)">Up Vote!</button>
<button (click)="upVoteClick(10)">Up Vote +10!</button>
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
@Component({
selector: 'app-person',
templateUrl: './person.component.html',
styleUrls: ['./person.component.css']
})
export class PersonComponent implements OnInit {
@Input() data;
@Output() upVote = new EventEmitter();
constructor() { }
ngOnInit() {
}
upVoteClick(inc) {
this.upVote.emit(inc);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment