<!DOCTYPE html>
<html>
<head>
<base href="." />
<title>angular2 playground</title>
<script data-require="tether@*" data-semver="1.3.4" src="//cdnjs.cloudflare.com/ajax/libs/tether/1.3.1/js/tether.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css" integrity="sha384-AysaV+vQoT3kOAXZkl02PThvDr8HYKPZhNT5h/CXfBThSRXQ6jW5DO2ekP5ViFdi" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Ubuntu" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="style.css" />
<script src="https://unpkg.com/zone.js/dist/zone.js"></script>
<script src="https://unpkg.com/zone.js/dist/long-stack-trace-zone.js"></script>
<script src="https://unpkg.com/reflect-metadata@0.1.3/Reflect.js"></script>
<script src="https://unpkg.com/systemjs@0.19.31/dist/system.js"></script>
<script src="config.js"></script>
<script>
System.import('app')
.catch(console.error.bind(console));
</script>
</head>
<body>
<my-app>
loading...
</my-app>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" integrity="sha384-3ceskX3iaEnIogmQchP8opvBy3Mi7Ce34nWjpBIwVTHfGYWQS9jwHDVRnpKKHJg7" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.3.7/js/tether.min.js" integrity="sha384-XTs3FgkjiBgo8qjEjBk0tGmf3wPrWtA6coPfQDfFEY8AnYJwjalXCiosYRBIBZX8" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/js/bootstrap.min.js" integrity="sha384-BLiI7JTZm+JWlgKa0M0kGRpJbF2J8q+qreVrKBC47e3K6BW78kGLrCkeRX6I9RoK" crossorigin="anonymous"></script>
</body>
</html>
/* Styles go here */
body {
font-family: 'Ubuntu', Arial, Helvetica, sans-serif; }
### Angular Starter Plunker - Typescript
System.config({
//use typescript for compilation
transpiler: 'typescript',
//typescript compiler options
typescriptOptions: {
emitDecoratorMetadata: true
},
paths: {
'npm:': 'https://unpkg.com/'
},
//map tells the System loader where to look for things
map: {
'app': './src',
'@angular/core': 'npm:@angular/core/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router/bundles/router.umd.js',
'@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
'@angular/core/testing': 'npm:@angular/core/bundles/core-testing.umd.js',
'@angular/common/testing': 'npm:@angular/common/bundles/common-testing.umd.js',
'@angular/compiler/testing': 'npm:@angular/compiler/bundles/compiler-testing.umd.js',
'@angular/platform-browser/testing': 'npm:@angular/platform-browser/bundles/platform-browser-testing.umd.js',
'@angular/platform-browser-dynamic/testing': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic-testing.umd.js',
'@angular/http/testing': 'npm:@angular/http/bundles/http-testing.umd.js',
'@angular/router/testing': 'npm:@angular/router/bundles/router-testing.umd.js',
'rxjs': 'npm:rxjs',
'typescript': 'npm:typescript@2.0.2/lib/typescript.js'
},
//packages defines our app package
packages: {
app: {
main: './main.ts',
defaultExtension: 'ts'
},
rxjs: {
defaultExtension: 'js'
}
}
});
//main entry point
import {AppModule} from './app';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
platformBrowserDynamic().bootstrapModule(AppModule)
//our root app component
import {Component, NgModule} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import { FormsModule } from '@angular/forms';
import { DemoComponent } from './demo.component'
import { ItemService } from './item.service'
@Component({
selector: 'my-app',
template: `
<div>
<h2>{{name}}</h2>
</div>
<br><br>
<demo></demo>
<br><br>
`,
})
export class App {
name:string;
constructor() {
this.name = 'Angular2 Demos'
}
}
@NgModule({
imports: [ BrowserModule, FormsModule ],
declarations: [ App, DemoComponent ],
providers: [ ItemService ],
bootstrap: [ App ]
})
export class AppModule {}
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import {Item} from './item';
import { ItemService } from './item.service';
@Component({
selector: 'demo',
template: `
<div>{{demoInfo}}</div>
<div>{{demoInstructions}}</div>
<div class="container-fluid" style="padding-top:50px">
<button class="btn btn-primary" (click)="createItem()"><span class="icon icon-plus"></span>Create</button>
<button class="btn btn-primary" (click)="getItems()"><span class="icon icon-cycle"></span>Refresh</button>
<form>
<div class="form-group" *ngFor="let item of items; let i=index" >
<div>
<div class="row" style="padding-top:40px;">
<div class="col-xs-12">
<label id="itemIdLabel-{{i}}" for="itemIdDiv-{{i}}">Id</label>
<div id="itemIdDiv-{{i}}">{{item.id}}</div>
</div>
</div>
<div class="row" style="padding-top:10px;">
<div class="col-xs-6 col-md-3">
<label id="itemNmLabel-{{i}}" for="itemNmInput-{{i}}">Name</label>
<input id="itemNmInput-{{i}}" name="itemNmInput-{{i}}" [(ngModel)]="item.nm" type="text" class="form-control">
</div>
<div class="col-xs-6 col-md-9">
<label id="itemDscLabel-{{i}}" for="itemDscInput-{{i}}">Description</label>
<input id="itemDscInput-{{i}}" name="itemDscInput-{{i}}" [(ngModel)]="item.dsc" type="text" class="form-control">
</div>
</div>
<div class="row" style="padding-top: 10px;">
<div class="col-xs-12">
<button class="btn btn-danger" (click)="deleteItem(item.id, i)"><span class="icon icon-cross"></span>Delete</button>
</div>
</div>
</div>
</div>
</form>
</div>
`,
})
export class DemoComponent implements OnInit{
demoInfo = `ngFor unshift bug`;
demoInstructions = `to see the behavior, watch item 001 carefully and push create to add a new item.
Refresh will reset the example`;
items: Item[];
constructor(private itemService: ItemService) {}
ngOnInit(){
this.getItems();
}
getItems(){
console.log("made it DemoComponent.getItems()");
this.itemService.getItems()
.subscribe(res => this.items = res);
}
createItem(){
//create a new item without an id. this will be committed once required fields are filled
//this works fine
//this.items.push(new Item("new", "newItem"));
//this does not
this.items.unshift(new Item("new", "newItem"));
}
deleteItem(id, index){
this.items.splice(index,1);
}
}
export class Item {
public id: string;
constructor(public nm: string,
public dsc: string){}
}
import { Injectable, OnInit } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Item } from './item';
@Injectable()
export class ItemService implements OnInit {
items = new Array<Item>();
constructor(){}
getItems(): Observable<Item[]> {
this.initItems();
return new Observable(observer => {
console.log(this.items);
observer.next(this.items);
observer.complete();
}
}
initItems(){
//simulating data coming back from a web service call with ids intact
this.items.length = 0;
this.items.push(new Item("first", "firstItem"));
this.items[0].id = '001';
this.items.push(new Item("second", "secondItem"));
this.items[1].id = '002';
this.items.push(new Item("third", "thirdItem"));
this.items[2].id = '003';
}
}