Skip to content

Instantly share code, notes, and snippets.

@hugo-dlb
Forked from anonymous/app.component.ts
Last active March 17, 2017 14:11
Show Gist options
  • Save hugo-dlb/363ffe27b9330fc33985d0c2403524b5 to your computer and use it in GitHub Desktop.
Save hugo-dlb/363ffe27b9330fc33985d0c2403524b5 to your computer and use it in GitHub Desktop.
simple product listing from json data with Angular 2
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<product-list></product-list>
`,
})
export class AppComponent { name = 'Angular'; }
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule } from '@angular/http';
import { ProductListComponent } from './components/product.list.component';
import { AppComponent } from './app.component';
@NgModule({
imports: [ BrowserModule, HttpModule ],
declarations: [ AppComponent, ProductListComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
[
{"id": 1,
"name": "product1"
},
{"id": 2,
"name": "product2"
},
{"id": 3,
"name": "product3"
}
]
{
"name": "angular-quickstart",
"version": "1.0.0",
"description": "QuickStart package.json from the documentation, supplemented with testing support",
"scripts": {
"build": "tsc -p src/",
"build:watch": "tsc -p src/ -w",
"build:e2e": "tsc -p e2e/",
"serve": "lite-server -c=bs-config.json",
"serve:e2e": "lite-server -c=bs-config.e2e.json",
"prestart": "npm run build",
"start": "concurrently \"npm run build:watch\" \"npm run serve\"",
"pree2e": "npm run build:e2e",
"e2e": "concurrently \"npm run serve:e2e\" \"npm run protractor\" --kill-others --success first",
"preprotractor": "webdriver-manager update",
"protractor": "protractor protractor.config.js",
"pretest": "npm run build",
"test": "concurrently \"npm run build:watch\" \"karma start karma.conf.js\"",
"pretest:once": "npm run build",
"test:once": "karma start karma.conf.js --single-run",
"lint": "tslint ./src/**/*.ts -t verbose"
},
"keywords": [],
"author": "",
"license": "MIT",
"dependencies": {
"@angular/common": "~2.4.0",
"@angular/compiler": "~2.4.0",
"@angular/core": "~2.4.0",
"@angular/forms": "~2.4.0",
"@angular/http": "~2.4.0",
"@angular/platform-browser": "~2.4.0",
"@angular/platform-browser-dynamic": "~2.4.0",
"@angular/router": "~3.4.0",
"angular-in-memory-web-api": "~0.2.4",
"systemjs": "0.19.40",
"core-js": "^2.4.1",
"rxjs": "5.0.1",
"zone.js": "^0.7.4"
},
"devDependencies": {
"concurrently": "^3.2.0",
"lite-server": "^2.2.2",
"typescript": "~2.0.10",
"canonical-path": "0.0.2",
"tslint": "^3.15.1",
"lodash": "^4.16.4",
"jasmine-core": "~2.4.1",
"karma": "^1.3.0",
"karma-chrome-launcher": "^2.0.0",
"karma-cli": "^1.0.1",
"karma-jasmine": "^1.0.2",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~4.0.14",
"rimraf": "^2.5.4",
"@types/node": "^6.0.46",
"@types/jasmine": "2.5.36"
},
"repository": {}
}
import { Component } from '@angular/core';
import { ProductListService } from '../services/product.list.service'
@Component({
selector: 'product-list',
template: `
<h1>Product list:</h1>
<div id='productlist'>
<div class='productbox' *ngFor="let product of products">{{ product.name }}</div>
</div>
`,
providers: [ProductListService]
})
export class ProductListComponent {
products = [];
constructor(private _productService: ProductListService){}
ngOnInit(){
this._productService.getProducts()
.subscribe(resProductsData => this.products = resProductsData);
}
}
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class ProductListService {
private _url: string = "http://localhost:3000/app/data.json";
constructor(private _http: Http){}
getProducts()
{
return this._http.get(this._url)
.map((response:Response) => response.json());
}
}
#productlist
{
position: relative;
width: 100%;
height: 100%;
}
.productbox
{
position: relative;
width: 100px;
height: 100px;
margin: 10px;
padding: 5px;
border-radius: 3px;
border: 1px solid black;
line-height: 100px;
text-align: center;
color: white;
background-color: lightskyblue;
display: inline-block;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment