import { Component } from '@angular/core';
import { User } from './shared/models/user';
@Component({
selector: 'my-app',
templateUrl: './app/app.component.html',
styleUrls: ['./app/app.component.css']
})
export class AppComponent {
message: string = 'Hello!';
users: User[] = [
{ id: 25, name: 'Chris', username: 'sevilayha' },
{ id: 26, name: 'Nick', username: 'whatnicktweets' },
{ id: 27, name: 'Holly', username: 'hollylawly' }
];
activeUser: User;
selectUser(user) {
this.activeUser = user;
console.log(this.activeUser);
}
onUserCreated(event) {
this.users.push(event.user);
}
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { UserProfileComponent } from './users/user-profile.component';
import { UserFormComponent } from './users/user-form.component';
@NgModule({
imports: [
BrowserModule,
FormsModule
],
declarations: [
AppComponent,
UserProfileComponent,
UserFormComponent
],
bootstrap: [ AppComponent ]
})
export class AppModule {}
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
const platform = platformBrowserDynamic();
platform.bootstrapModule(AppModule);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My Angular App!</title>
<!-- css -->
<!-- load bootstrap and some simple css -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<style>body { padding: 50px 0; }</style>
<!-- js -->
<!-- load polyfills and angular dependencies -->
<script src="https://unpkg.com/core-js/client/shim.min.js"></script>
<script src="https://unpkg.com/zone.js@0.6.23?main=browser"></script>
<script src="https://unpkg.com/reflect-metadata@0.1.3"></script>
<!-- load our angular app with systemjs -->
<script src="https://unpkg.com/systemjs@0.19.27/dist/system.src.js"></script>
<script src="systemjs.config.js"></script>
<script>
System.import('app').catch(function(err) { console.error(err); });
</script>
</head>
<body class="container">
<my-app></my-app>
</body>
</html>
## Getting Started with Angular 2
Code for the [Scotch School](https://school.scotch.io) course: [Getting Started with Angular 2](https://school.scotch.io/getting-started-with-angular-2).
/**
* PLUNKER VERSION
* (based on systemjs.config.js in angular.io)
* System configuration for Angular 2 samples
* Adjust as necessary for your application needs.
*/
(function (global) {
System.config({
// DEMO ONLY! REAL CODE SHOULD NOT TRANSPILE IN THE BROWSER
transpiler: 'ts',
typescriptOptions: {
tsconfig: true
},
meta: {
'typescript': {
"exports": "ts"
}
},
paths: {
// paths serve as alias
'npm:': 'https://unpkg.com/'
},
// map tells the System loader where to look for things
map: {
// our app is within the app folder
app: 'app',
// angular bundles
'@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/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.js',
// other libraries
'rxjs': 'npm:rxjs',
'angular2-in-memory-web-api': 'npm:angular2-in-memory-web-api',
'ts': 'npm:plugin-typescript@4.0.10/lib/plugin.js',
'typescript': 'npm:typescript@2.0.2/lib/typescript.js',
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
app: {
main: './main.ts',
defaultExtension: 'ts'
},
rxjs: {
defaultExtension: 'js'
},
'angular2-in-memory-web-api': {
main: './index.js',
defaultExtension: 'js'
}
}
});
})(this);
/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true
}
}
import { Component, Output, EventEmitter } from '@angular/core';
import { User } from '../shared/models/user';
@Component({
selector: 'user-form',
styles: [`
form {
padding: 10px;
background: #ECF0F1;
border-radius: 3px;
margin-bottom: 30px;
}
`],
template: `
<form #form="ngForm" (ngSubmit)="onSubmit()" *ngIf="active">
<div class="form-group" [ngClass]="{ 'has-error': name.invalid && name.touched }">
<input type="text" class="form-control" name="name" placeholder="Name" required
[(ngModel)]="newUser.name" #name="ngModel">
<span class="help-block" *ngIf="name.invalid && name.touched">Name is required.</span>
</div>
<div class="form-group" [ngClass]="{ 'has-error': username.invalid && username.touched }">
<input type="text" class="form-control" name="username" placeholder="Username" required
[(ngModel)]="newUser.username" #username="ngModel">
<span class="help-block" *ngIf="username.invalid && username.touched">Username is required.</span>
</div>
<button type="submit" class="btn btn-lg btn-block btn-primary" [disabled]="form.invalid">
Create User
</button>
</form>
`
})
export class UserFormComponent {
@Output() userCreated = new EventEmitter();
newUser: User = new User();
active: boolean = true;
onSubmit() {
// show the event that the user was created
this.userCreated.emit({ user: this.newUser });
this.newUser = new User();
this.active = false;
setTimeout(() => this.active = true, 0);
}
}
import { Component, Input } from '@angular/core';
import { User } from '../shared/models/user';
@Component({
selector: 'user-profile',
template: `
<div class="jumbotron" *ngIf="user">
<h2>{{ user.name }} <small>{{ user.username }}</small></h2>
<input class="form-control" [(ngModel)]="user.name">
</div>
`
})
export class UserProfileComponent {
@Input() user: User;
}
export class User {
id: number;
name: string;
username: string;
}
<header>
<nav class="navbar navbar-inverse">
<div class="navbar-header">
<a href="/" class="navbar-brand">My Angular 2 App!</a>
</div>
</nav>
</header>
<main>
<div class="row">
<div class="col-sm-4">
<div *ngIf="users">
<ul class="list-group users-list">
<li class="list-group-item"
*ngFor="let user of users"
(click)="selectUser(user)"
[class.active]="user === activeUser">
{{ user.name }} ({{ user.username }})
</li>
</ul>
</div>
<user-form (userCreated)="onUserCreated($event)"></user-form>
</div>
<div class="col-sm-8">
<user-profile [user]="activeUser"></user-profile>
<div class="jumbotron" *ngIf="!activeUser">
<span class="glyphicon glyphicon-hand-left"></span>
<h2>Choose a User</h2>
</div>
</div>
</div>
</main>
<footer class="text-center">
Copyright © 2016
</footer>
.users-list li {
cursor: pointer;
}
.jumbotron .glyphicon {
font-size: 80px;
}