Skip to content

Instantly share code, notes, and snippets.

@eliyas5044
Created October 6, 2016 17:08
Show Gist options
  • Save eliyas5044/fa2ee945e7d4741e98a8e9151b504a1a to your computer and use it in GitHub Desktop.
Save eliyas5044/fa2ee945e7d4741e98a8e9151b504a1a to your computer and use it in GitHub Desktop.
angular front-end with laravel 5.3 back-end
import {Component, OnInit} from "@angular/core";
import {ArticleService} from "./article.service";
import {Article} from "./article";
@Component({
moduleId: module.id,
selector: 'article',
template: `
<h1>All Articles</h1>
<hr>
<div *ngFor="let article of articles">
<h2>{{article.title}}</h2>
<p>{{article.body}}</p>
</div>
` ,
providers: [ArticleService]
})
export class ArticleComponent implements OnInit {
articles: Article[];
errorMessage: string;
mode: 'Observable';
constructor(private articleService : ArticleService){}
ngOnInit(){this.getArticles()}
getArticles(){
this.articleService.getArticles()
.subscribe(
articles => this.articles = articles,
error => this.errorMessage = <any>error
);
}
}
import {Injectable} from "@angular/core";
import {Jsonp, Response} from "@angular/http";
import {Observable} from "rxjs/Observable";
import {Article} from "./article";
@Injectable()
export class ArticleService {
private articleUrl = 'http://localhost/angu_lara/public/articles';
constructor(private jsonp: Jsonp){}
getArticles (): Observable<Article[]> {
return this.jsonp.get(this.articleUrl)
.map(this.extractData)
.catch(this.handleError);
}
private extractData(res: Response){
let body = res.json();
return body || {};
}
private handleError(error: any){
let errMsg = (error.message) ? error.message:
error.status ? `${error.status}-${error.statusText}`:`Server error`;
console.log(errMsg);
return Observable.throw(errMsg);
}
}
export class Article{
constructor(
public id: number,
public title: string,
public body: string
){}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment