Skip to content

Instantly share code, notes, and snippets.

@elcodabra
Last active September 20, 2017 11:42
Show Gist options
  • Save elcodabra/c53a61b1ae935b390dbff63d5511ade8 to your computer and use it in GitHub Desktop.
Save elcodabra/c53a61b1ae935b390dbff63d5511ade8 to your computer and use it in GitHub Desktop.
New Item for Angular
<form (ngSubmit)="onAdd()">
<div class="form-group">
<input
type="text" name="content"
placeholder={{placeholder}}
[(ngModel)]="model.content">
</input>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'app-new-tweet',
templateUrl: './new-tweet.component.html'
})
export class NewTweetComponent implements OnInit {
@Input() placeholder: string = "";
@Output() add = new EventEmitter<any>();
model = {content: ''};
constructor() { }
ngOnInit() {}
onAdd() {
return this.add.emit(this.model)
}
}
<div>
<ul class="list-unstyled">
<li class="media">
<div class="media-body">
<app-new-tweet placeholder="Add your text..." (add)="onAdd($event)"></app-new-tweet>
</div>
</li>
<!-- List items -->
</ul>
</div>
// ...import
@Component({
selector: 'app-home',
templateUrl: './tweets-list.component.html',
styleUrls: ['./tweets-list.component.css']
})
export class ListTweetsComponent implements OnInit {
// ...constructor, ngOnInit
onAdd(tweet) {
this.backendService.addTweet(tweet)
.then((data) => {
if (data && data.createTweet) this.tweets.unshift(data.createTweet);
})
.catch((error) => {
console.log(error);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment