Last active
September 20, 2017 11:42
-
-
Save elcodabra/c53a61b1ae935b390dbff63d5511ade8 to your computer and use it in GitHub Desktop.
New Item for Angular
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ...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