I have create two component: 1. create-articles : is used to create article. 2. List Articles : is used to list all the articles.
Parent component is Home Component
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
constructor() {
}
ngOnInit() {
}
}
<div class="container">
<div class="row">
<div class="col-md-4">
<articles-form></articles-form>
</div>
<div class="col-md-4">
<articles></articles>
</div>
</div>
</div>
i want to reresh the article list component whenever article is created.
import { Component, OnInit } from '@angular/core';
import { ArticlesService } from '../../services/articles.service';
import { Article } from '../../models/article.model';
import { IArticles } from '../../interfaces/IArticles';
import { Observable } from 'rxjs';
@Component({
selector: 'articles',
templateUrl: './articles.component.html',
styleUrls: ['./articles.component.css'],
providers:[ArticlesService]
})
export class ArticlesComponent implements OnInit {
articles: IArticles[];
message:string;
errorMessage:string;
constructor(private as:ArticlesService) { }
ngOnInit():void {
this.fetchArticles();
}
fetchArticles(): void {
this.as.getArticles()
.subscribe( articles => {
this.articles = articles
console.log(this.articles);
},
error => this.errorMessage = <any>error);
};
}
<button class="btn btn-primary" (click)="fetchArticles()">
Reload Data
</button>
<div class="table table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th>#</th>
<th>Title</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let article of articles;let i=index">
<td>
{{i+1}}
</td>
<td>
{{article.articles.title}}
</td>
</tr>
</tbody>
</table>
</div>
import { Component, OnInit } from '@angular/core';
import { ArticlesService } from '../../services/articles.service';
import { Article } from '../../models/article.model';
import { IArticles } from '../../interfaces/IArticles';
import { Observable } from 'rxjs';
import { ArticlesComponent } from '../articles/articles.component';
import { EventEmitter, Input, Output } from '@angular/core';
@Component({
selector: 'articles-form',
templateUrl: './articles-form.component.html',
styleUrls: ['./articles-form.component.css'],
providers:[ArticlesService]
})
export class ArticlesFormComponent implements OnInit {
books: IArticles[];
article:IArticles=new Article(1,"Let Us C","Rahul Shaw","http://google.com");
message:string;
errorMessage:string;
articles:IArticles[];
constructor(private as:ArticlesService) { }
ngOnInit():void {}
onSubmit(data:IArticles) : void{
var articles=this.as.createArticles(data)
.subscribe( book => {
this.message = "submitted";
},
error => this.errorMessage = <any>error);
};
}
<div class="panel panel-primary">
<div class="panel-body">
<h1>Article Posting</h1>
<form (ngSubmit)="onSubmit(article)">
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" id="title" required [(ngModel)]="article.title" name="title">
</div>
<div class="form-group">
<label for="author">Author</label>
<input type="text" class="form-control" id="author" required [(ngModel)]="article.author" name="author">
</div>
<div class="form-group">
<label for="url">URL</label>
<input type="text" class="form-control" id="url" [(ngModel)]="article.url" name="url">
</div>
<button type="submit" class="btn btn-default"> Submit
</button>
{{ name }}
</form>
</div>
</div>
i want to refresh the article list component whenever article is created.