Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

02 01b #13

Open
wants to merge 187 commits into
base: 02_01b
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
187 commits
Select commit Hold shift + click to select a range
768e043
end of video commit
jschwarty Jul 6, 2016
e228f8b
Merge branch '02_01b' of https://github.com/LyndaExerciseFiles/angula…
jschwarty Jul 6, 2016
627c532
end of video commit
jschwarty Jul 6, 2016
233516f
end of video commit
jschwarty Jul 6, 2016
0ffac31
end of video commit
jschwarty Jul 6, 2016
82b76f4
end of video commit
jschwarty Jul 6, 2016
05dbce6
end of video commit
jschwarty Jul 6, 2016
5b1f1c2
end of video commit
jschwarty Jul 6, 2016
5703f09
end of video commit
jschwarty Jul 6, 2016
38e8eea
end of video commit
jschwarty Jul 6, 2016
21d5243
Merge branch '02_01b' into 02_02b
Jul 16, 2016
428e7e8
Merge branch '02_02b' into 02_03b
Jul 16, 2016
a2ec9c4
Merge branch '02_03b' into 02_04b
Jul 16, 2016
fb1d486
Merge branch '02_04b' into 02_05b
Jul 16, 2016
76c95aa
Merge branch '02_05b' into 02_06b
Jul 16, 2016
f270375
Merge branch '02_06b' into 02_07b
Jul 16, 2016
6a07034
Merge branch '02_07b' into 02_08b
Jul 16, 2016
8f50043
Merge branch '02_08b' into 02_09b
Jul 16, 2016
d9a6ff0
Merge branch '02_09b' into 02_10b
Jul 16, 2016
bf21a79
end of video commit
Jul 16, 2016
8b1f537
end of video commit
Jul 16, 2016
3d68078
end of video commit
Jul 16, 2016
22e5270
end of video commit
Jul 16, 2016
9ab79ab
end of video commit
Jul 16, 2016
d6d72a7
end of video commit
Jul 16, 2016
e249ccb
end of video commit
Jul 16, 2016
a5cd3a5
end of video commit
Jul 16, 2016
bc2dc6b
end of video commit
Jul 16, 2016
faa71e9
end of video commit
Jul 16, 2016
c1dcd4b
end of video commit
Jul 16, 2016
67a5a4e
end of video commit
Jul 16, 2016
00f39e9
end of video commit
Jul 16, 2016
0162a27
end of video commit
Jul 16, 2016
a9d0ec9
end of video commit
Jul 16, 2016
7932afa
end of video commit
Jul 16, 2016
4aa2ccb
end of video commit
Jul 16, 2016
71ba816
end of video commit
Jul 16, 2016
abc514b
end of video commit
Jul 16, 2016
2444825
end of video commit
Jul 16, 2016
27d7fd2
fix xhr backend get method
Jul 16, 2016
e1021d6
end of video commit
Jul 16, 2016
6c77993
end of video commit
Jul 16, 2016
bce1981
end of video commit
Jul 16, 2016
548a14e
end of video commit
Jul 16, 2016
a33513d
end of video commit
Jul 16, 2016
4c7f833
end of video commit
Jul 16, 2016
cc9d90c
end of video commit
Jul 16, 2016
918c7f3
end of video commit
Jul 16, 2016
37edbc1
add missing http script tag
Jul 16, 2016
c0bda88
Merge pull request #1 from LyndaExerciseFiles/06_02b
jschwarty Jul 16, 2016
8ce30b2
Merge pull request #2 from LyndaExerciseFiles/06_03b
jschwarty Jul 16, 2016
ebbe979
Merge pull request #3 from LyndaExerciseFiles/06_04b
jschwarty Jul 16, 2016
f6aec04
Merge pull request #4 from LyndaExerciseFiles/06_05b
jschwarty Jul 16, 2016
09436b4
Merge pull request #5 from LyndaExerciseFiles/07_01b
jschwarty Jul 16, 2016
ae3bdcb
add missing router script tag
Jul 16, 2016
6131e15
Merge pull request #6 from LyndaExerciseFiles/07_02b
jschwarty Jul 16, 2016
470bac1
Merge pull request #7 from LyndaExerciseFiles/07_03b
jschwarty Jul 16, 2016
3041563
Merge pull request #8 from LyndaExerciseFiles/07_04b
jschwarty Jul 16, 2016
c56ed9f
Merge pull request #9 from LyndaExerciseFiles/07_05b
jschwarty Jul 16, 2016
5cb78e8
Merge branch '02_01b' into 02_02b
jschwarty Jul 28, 2016
b87fd5d
upgrade code to support rc4
jschwarty Jul 28, 2016
8bdf8b1
Merge branch '02_02b' into 02_03b
jschwarty Jul 28, 2016
108a02f
Merge branch '02_03b' into 02_04b
jschwarty Jul 28, 2016
bd496ed
Merge branch '02_04b' into 02_05b
jschwarty Jul 28, 2016
0930671
upgrade code to support rc4
jschwarty Jul 28, 2016
ce98911
Merge branch '02_05b' of https://github.com/LyndaExerciseFiles/angula…
jschwarty Jul 28, 2016
acb48ba
Merge branch '02_05b' into 02_06b
jschwarty Jul 28, 2016
849665a
upgrade code to support rc4
jschwarty Jul 28, 2016
ce4e091
Merge branch '02_06b' into 02_07b
jschwarty Jul 28, 2016
b674b21
fix spacing
jschwarty Jul 28, 2016
8b5ddb4
Merge branch '02_02b' into 02_03b
jschwarty Jul 28, 2016
350964f
Merge branch '02_03b' into 02_04b
jschwarty Jul 28, 2016
5e9a45a
Merge branch '02_04b' into 02_05b
jschwarty Jul 28, 2016
41fba23
Merge branch '02_05b' into 02_06b
jschwarty Jul 28, 2016
09ad618
Merge branch '02_06b' into 02_07b
jschwarty Jul 28, 2016
a5edb2d
Merge branch '02_07b' into 02_08b
jschwarty Jul 28, 2016
6f4776f
Merge branch '02_08b' into 02_09b
jschwarty Jul 28, 2016
389cb20
Merge branch '02_09b' into 02_10b
jschwarty Jul 28, 2016
f02de73
Merge branch '02_01b' into 02_02b
jschwarty Jul 28, 2016
1ed4403
Merge branch '02_02b' into 02_03b
jschwarty Jul 28, 2016
90edce1
Merge branch '02_03b' into 02_04b
jschwarty Jul 28, 2016
2e0edc1
Merge branch '02_01b' into 02_02b
jschwarty Jul 28, 2016
2178150
Merge branch '02_02b' into 02_03b
jschwarty Jul 28, 2016
a4c0be8
Merge branch '02_03b' into 02_04b
jschwarty Jul 28, 2016
62093d3
Merge branch '02_04b' into 02_05b
jschwarty Jul 28, 2016
1a2aefd
Merge branch '02_05b' into 02_06b
jschwarty Jul 28, 2016
31257d2
Merge branch '02_06b' into 02_07b
jschwarty Jul 28, 2016
96fc7f5
Merge branch '02_07b' into 02_08b
jschwarty Jul 28, 2016
2bfaaac
Merge branch '02_08b' into 02_09b
jschwarty Jul 28, 2016
8878e28
Merge branch '02_09b' into 02_10b
jschwarty Jul 28, 2016
6e95f56
Merge branch '02_10b' into 03_01b
jschwarty Jul 28, 2016
80da668
Merge branch '03_01b' into 03_02b
jschwarty Jul 28, 2016
633e92a
update code for rc4
jschwarty Jul 28, 2016
997ed86
Merge branch '03_02b' into 03_03b
jschwarty Jul 28, 2016
e083ea0
update code for rc4
jschwarty Jul 28, 2016
caaa2cb
Merge branch '03_03b' into 03_04b
jschwarty Jul 28, 2016
c9f1663
fix spacing
jschwarty Jul 28, 2016
c10caff
Merge branch '03_02b' into 03_03b
jschwarty Jul 28, 2016
3852cee
Merge branch '03_03b' into 03_04b
jschwarty Jul 28, 2016
8a436d9
Merge branch '03_04b' into 03_05b
jschwarty Jul 28, 2016
fdfa657
update code for rc4
jschwarty Jul 28, 2016
c06f696
Merge branch '03_05b' into 03_06b
jschwarty Jul 28, 2016
55f9450
remove template container
jschwarty Jul 28, 2016
e2b03ee
Merge branch '03_02b' into 03_03b
jschwarty Jul 28, 2016
aa96562
Merge branch '03_03b' into 03_04b
jschwarty Jul 28, 2016
51531d5
Merge branch '03_04b' into 03_05b
jschwarty Jul 28, 2016
29b80ae
fix merge issue
jschwarty Jul 28, 2016
5ded063
Merge branch '03_05b' into 03_06b
jschwarty Jul 28, 2016
3dd1c0f
Merge branch '03_06b' into 03_07b
jschwarty Jul 28, 2016
787ca82
Merge branch '03_07b' into 03_08b
jschwarty Jul 28, 2016
fbaaf61
Merge branch '03_08b' into 04_02b
jschwarty Jul 28, 2016
5086822
update code for rc4 and new forms
jschwarty Jul 28, 2016
f54c340
Merge branch '04_02b' into 04_03b
jschwarty Jul 28, 2016
6cf2d20
update for new forms
jschwarty Jul 28, 2016
0c3bf29
Merge branch '04_03b' into 04_04b
jschwarty Jul 28, 2016
d7dfeb1
update for new forms
jschwarty Jul 28, 2016
d27477e
Merge branch '04_04b' into 04_05b
jschwarty Jul 28, 2016
fb1ea94
Merge branch '04_05b' into 04_06b
jschwarty Jul 28, 2016
0763e55
Merge branch '04_06b' into 05_03b
jschwarty Jul 28, 2016
3b90808
fix merge issue and add new forms support
jschwarty Jul 28, 2016
b5c0ef0
Merge branch '05_03b' into 05_04b
jschwarty Jul 28, 2016
2fbb07a
update for formbuilder all the way
jschwarty Jul 28, 2016
e560616
Merge branch '05_04b' into 05_05b
jschwarty Jul 28, 2016
3079527
Merge branch '05_05b' into 05_06b
jschwarty Jul 28, 2016
355e2b6
Merge branch '05_06b' into 05_07b
jschwarty Jul 28, 2016
78f9398
Merge branch '05_07b' into 06_01b
jschwarty Jul 28, 2016
d29ee71
update code for rc4
jschwarty Jul 28, 2016
ba7a1f7
Merge branch '06_01b' into 06_02b
jschwarty Jul 28, 2016
d0dd8d3
fix missing parenthesis
jschwarty Jul 28, 2016
c0309c0
Merge branch '06_02b' into 06_03b
jschwarty Jul 28, 2016
f5cf555
Merge branch '06_03b' into 06_04b
jschwarty Jul 28, 2016
83aac48
update code for rc4
jschwarty Jul 28, 2016
dbfed97
Merge branch '06_04b' into 06_05b
jschwarty Jul 28, 2016
3f2282d
Merge branch '06_05b' into 07_01b
jschwarty Jul 28, 2016
479d0cb
remove problematic 404 response
jschwarty Jul 28, 2016
9852f83
Merge branch '06_01b' into 06_02b
jschwarty Jul 28, 2016
6d9d025
Merge branch '06_02b' into 06_03b
jschwarty Jul 28, 2016
23c1a50
Merge branch '06_03b' into 06_04b
jschwarty Jul 28, 2016
0138d3b
Merge branch '06_04b' into 06_05b
jschwarty Jul 28, 2016
5a6d9fe
Merge branch '06_05b' into 07_01b
jschwarty Jul 28, 2016
7c2af06
add missing semicolon
jschwarty Jul 28, 2016
137f84f
Merge branch '04_04b' into 04_05b
jschwarty Jul 28, 2016
614d4f8
Merge branch '04_05b' into 04_06b
jschwarty Jul 28, 2016
3f7bd4b
Merge branch '04_06b' into 05_03b
jschwarty Jul 28, 2016
3116f46
Merge branch '05_03b' into 05_04b
jschwarty Jul 28, 2016
ca5e639
Merge branch '05_04b' into 05_05b
jschwarty Jul 28, 2016
69e157b
Merge branch '05_05b' into 05_06b
jschwarty Jul 28, 2016
fca6533
Merge branch '05_06b' into 05_07b
jschwarty Jul 28, 2016
26fabfe
Merge branch '05_07b' into 06_01b
jschwarty Jul 28, 2016
1e978f3
Merge branch '06_01b' into 06_02b
jschwarty Jul 28, 2016
1ac0c04
Merge branch '06_02b' into 06_03b
jschwarty Jul 28, 2016
a5550e7
Merge branch '06_03b' into 06_04b
jschwarty Jul 28, 2016
67a3f04
Merge branch '06_04b' into 06_05b
jschwarty Jul 28, 2016
37f465b
Merge branch '06_05b' into 07_01b
jschwarty Jul 28, 2016
d7fc782
update to RC5
jschwarty Aug 10, 2016
bdc3cf5
fix spacing
jschwarty Aug 10, 2016
9d76f70
fix inputs and outputs
jschwarty Aug 10, 2016
b7d9896
fix spacing
jschwarty Aug 10, 2016
f453b72
fix spacing
jschwarty Aug 10, 2016
71c6596
fix spacing and formatting
jschwarty Aug 10, 2016
7d0cda6
fix inputs and outputs
jschwarty Aug 10, 2016
9e65fae
Merge branch '05_04b' into 05_05b
jschwarty Aug 11, 2016
27e0b69
add media item service
jschwarty Aug 11, 2016
3de20fa
fix merge issues
jschwarty Aug 11, 2016
658d494
remove early edition of service use
jschwarty Aug 11, 2016
9325e1f
Merge branch '05_05b' into 05_06b
jschwarty Aug 11, 2016
c8c17c4
Merge branch '05_06b' into 05_07b
jschwarty Aug 11, 2016
0a3eac0
udpate to RC5
jschwarty Aug 11, 2016
ecdeefe
Merge branch '05_07b' into 06_01b
jschwarty Aug 11, 2016
6666bb2
update for RC5
jschwarty Aug 11, 2016
0b1d75c
Merge branch '06_01b' into 06_02b
jschwarty Aug 12, 2016
37e3c4d
update to RC5
jschwarty Aug 12, 2016
3e4826c
Merge branch '06_02b' into 06_03b
jschwarty Aug 12, 2016
193d008
Merge branch '06_03b' into 06_04b
jschwarty Aug 12, 2016
66b6297
fix spacing
jschwarty Aug 12, 2016
8fee50b
Merge branch '06_04b' into 06_05b
jschwarty Aug 12, 2016
13fe7b3
Merge branch '06_05b' into 07_01b
jschwarty Aug 12, 2016
6afe623
Merge branch '07_01b' into 07_02b
jschwarty Aug 16, 2016
8503376
Merge branch '07_02b' into 07_03b
jschwarty Aug 16, 2016
96a079a
add new router stuff for registering routes
jschwarty Aug 16, 2016
8bd9f9e
Merge branch '07_03b' into 07_04b
jschwarty Aug 16, 2016
52836ec
fix spacing
jschwarty Aug 16, 2016
d9373e1
Merge branch '07_04b' into 07_05b
jschwarty Aug 16, 2016
57a568a
Merge branch '07_05b'
jschwarty Aug 16, 2016
c569897
end of video commit
jschwarty Sep 26, 2016
2b73fca
final video commit
jschwarty Sep 26, 2016
10c97ba
Update README.md
jschwarty Jul 13, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ typings
app/**/*.js
app/**/*.js.map
npm-debug.log*
.DS_store
.DS_store
.idea
53 changes: 3 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,51 +1,4 @@
# Angular 2 Essential Training
# UPDATE
The repository for the **Angular Essential Training** course can be found at https://github.com/coursefiles/angular2-essential-training.

This is the repository for my course, [Angular 2 Essential Training](https://www.lynda.com/AngularJS-tutorials/AngularJS-2-Essential-Training/422834-2.html).
The full course is available at [lynda.com](https://lynda.com).

## Course Description

JavaScript frameworks help you code more quickly, by providing special functionality for developing specific types of web projects. Angular was designed by Google to address challenges programmers face building single-page applications. This course introduces you to the essentials of this "superheroic" framework, including declarative templates, two-way data binding, and dependency injection. Justin Schwartzenberger steps through the framework one feature at a time, focusing on the new component-based architecture of Angular 2. After completing this training, you'll be able to tackle the other project-based courses in our library and create your own Angular app.

Topics include:
- What is Angular?
- Setting up an Angular template
- Creating a component
- Displaying data
- Working with events
- Using two-way data binding
- Creating a subcomponent
- Using the built-in HTTP module
- Using the built-in router module

## Instructions

1. Make sure you have these installed
- [node.js](http://nodejs.org/)
- [git](http://git-scm.com/)

2. Clone this repository into your local machine using the terminal (mac) or Gitbash (PC)

`git clone https://github.com/LyndaExerciseFiles/angular2-essential-training.git`

3. CD to the folder

`cd angular2-essential-training`

4. Run the following to install the project dependencies:

`npm install`

5. Run the npm start command to build the code, watch for file changes, and serve up the site locally:

`npm start`

Note that the site will run using `lite-server` and will be served up at the following local address:
http://localhost:3000

*If you use a code editor that launches its own web server please note that it may run on a different port number.
You will want to use `npm start` for this project.*

## More Stuff
Check out some of my [other courses on lynda.com](https://lynda.com/justinschwartzenberger).
You can also [follow me on twitter](https://twitter.com/schwarty), or read [my blog](http://schwarty.com).
*This is the old location of the code and is no longer being maintained*
30 changes: 30 additions & 0 deletions app/app.component.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
:host {
display: flex;
font-family: Arial, Helvetica, sans-serif;
min-height: 100%;
}
nav {
display: flex;
flex-direction: column;
width: 68px;
background-color: #53ace4;
}
nav .icon {
width: 48px;
height: 48px;
margin: 10px;
}
section {
width: 100%;
background-color: #32435b;
}
section > header {
color: #ffffff;
padding: 10px;
}
section > header > h1 {
font-size: 2em;
}
section > header .description {
font-style: italic;
}
18 changes: 18 additions & 0 deletions app/app.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<nav>
<a routerLink="/">
<img src="media/04.png" class="icon" />
</a>
<a routerLink="/Movies">
<img src="media/03.png" class="icon" />
</a>
<a routerLink="/Series">
<img src="media/02.png" class="icon" />
</a>
</nav>
<section>
<header>
<h1>Media Watch List</h1>
<p class="description">Keeping track of the media I want to watch.</p>
</header>
<router-outlet></router-outlet>
</section>
8 changes: 8 additions & 0 deletions app/app.component.ts
100644 → 100755
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Component } from '@angular/core';

@Component({
selector: 'mw-app',
templateUrl: 'app/app.component.html',
styleUrls: ['app/app.component.css']
})
export class AppComponent { }
41 changes: 41 additions & 0 deletions app/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { ReactiveFormsModule } from '@angular/forms';
import { HttpModule, XHRBackend } from '@angular/http';

import { AppComponent } from './app.component';
import { MediaItemComponent } from './media-item.component';
import { MediaItemListComponent } from './media-item-list.component';
import { FavoriteDirective } from './favorite.directive';
import { CategoryListPipe } from './category-list.pipe';
import { MediaItemFormComponent } from './media-item-form.component';
import { MediaItemService } from './media-item.service';
import { lookupListToken, lookupLists } from './providers';
import { MockXHRBackend } from './mock-xhr-backend';
import { routing } from './app.routing';

@NgModule({
imports: [
BrowserModule,
ReactiveFormsModule,
HttpModule,
routing
],
declarations: [
AppComponent,
MediaItemComponent,
MediaItemListComponent,
FavoriteDirective,
CategoryListPipe,
MediaItemFormComponent
],
providers: [
MediaItemService,
{ provide: lookupListToken, useValue: lookupLists },
{ provide: XHRBackend, useClass: MockXHRBackend }
],
bootstrap: [
AppComponent
]
})
export class AppModule {}
12 changes: 12 additions & 0 deletions app/app.routing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Routes, RouterModule } from '@angular/router';

import { MediaItemFormComponent } from './media-item-form.component';
import { MediaItemListComponent } from './media-item-list.component';

const appRoutes: Routes = [
{ path: 'add', component: MediaItemFormComponent },
{ path: ':medium', component: MediaItemListComponent },
{ path: '', pathMatch: 'full', redirectTo: 'all' }
];

export const routing = RouterModule.forRoot(appRoutes);
16 changes: 16 additions & 0 deletions app/category-list.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Pipe } from '@angular/core';

@Pipe({
name: 'categoryList'
})
export class CategoryListPipe {
transform(mediaItems) {
var categories = [];
mediaItems.forEach(mediaItem => {
if (categories.indexOf(mediaItem.category) <= -1) {
categories.push(mediaItem.category);
}
});
return categories.join(', ');
}
}
22 changes: 22 additions & 0 deletions app/favorite.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Directive, HostBinding, HostListener, Input } from '@angular/core';

@Directive({
selector: '[mwFavorite]'
})
export class FavoriteDirective {
@HostBinding('class.is-favorite') isFavorite = true;

@HostBinding('class.is-favorite-hovering') hovering = false;

@HostListener('mouseenter') onMouseEnter() {
this.hovering = true;
}

@HostListener('mouseleave') onMouseLeave() {
this.hovering = false;
}

@Input() set mwFavorite(value) {
this.isFavorite = value;
}
}
4 changes: 4 additions & 0 deletions app/main.ts
100644 → 100755
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';

platformBrowserDynamic().bootstrapModule(AppModule);
52 changes: 52 additions & 0 deletions app/media-item-form.component.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
:host {
display: block;
padding: 10px;
}
ul {
list-style-type: none;
}
ul li {
margin: 10px 0;
}
header, label {
color: #53ace4;
}
input, select {
background-color: #29394b;
color: #c6c5c3;
border-radius: 3px;
border: none;
box-shadow: 0 1px 2px rgba(0,0,0,0.2) inset, 0 -1px 0 rgba(0,0,0,0.05) inset;
border-color: #53ace4;
padding: 6px;
}
.ng-invalid:not(form):not(.ng-pristine):not(.required-invalid) {
border: 1px solid #d93a3e;
}
input[required].ng-invalid {
border-right: 5px solid #d93a3e;
}
input[required]:not(.required-invalid),
input[required].ng-invalid:not(.required-invalid) {
border-right: 5px solid #37ad79;
}
.error {
color: #d93a3e;
}
#year {
width: 50px;
}
button[type=submit] {
background-color: #45bf94;
border: 0;
padding: 10px;
font-size: 1em;
border-radius: 4px;
color: #ffffff;
cursor: pointer;
}
button[type=submit]:disabled {
background-color: #333;
color: #666;
cursor: default;
}
44 changes: 44 additions & 0 deletions app/media-item-form.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<header>
<h2>Add Media to Watch</h2>
</header>
<form
[formGroup]="form"
(ngSubmit)="onSubmit(form.value)">
<ul>
<li>
<label for="medium">Medium</label>
<select name="medium" id="medium" formControlName="medium">
<option *ngFor="let medium of lookupLists.mediums" value="{{medium}}">{{medium}}</option>
</select>
</li>
<li>
<label for="name">Name</label>
<input type="text" name="name" id="name" formControlName="name">
<div *ngIf="form.controls.name.errors?.pattern" class="error">
Name has invalid characters
</div>
</li>
<li>
<label for="category">Category</label>
<select name="category" id="category" formControlName="category">
<option value="Action">Action</option>
<option value="Science Fiction">Science Fiction</option>
<option value="Comedy">Comedy</option>
<option value="Drama">Drama</option>
<option value="Horror">Horror</option>
<option value="Romance">Romance</option>
</select>
</li>
<li>
<label for="year">Year</label>
<input type="text" name="year" id="year" maxlength="4" formControlName="year">
<div *ngIf="form.controls.year.errors?.year" class="error">
Must be between
{{form.controls.year.errors?.year.min}}
and
{{form.controls.year.errors?.year.max}}
</div>
</li>
</ul>
<button type="submit" [disabled]="!form.valid">Save</button>
</form>
59 changes: 59 additions & 0 deletions app/media-item-form.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Component, Inject } from '@angular/core';
import { Validators, FormBuilder } from '@angular/forms';
import { Router } from '@angular/router';

import { MediaItemService } from './media-item.service';
import { lookupListToken } from './providers';

@Component({
selector: 'mw-media-item-form',
templateUrl: 'app/media-item-form.component.html',
styleUrls: ['app/media-item-form.component.css']
})
export class MediaItemFormComponent {
form;

constructor(
private formBuilder: FormBuilder,
private mediaItemService: MediaItemService,
@Inject(lookupListToken) public lookupLists,
private router: Router) {}

ngOnInit() {
this.form = this.formBuilder.group({
medium: this.formBuilder.control('Movies'),
name: this.formBuilder.control('', Validators.compose([
Validators.required,
Validators.pattern('[\\w\\-\\s\\/]+')
])),
category: this.formBuilder.control(''),
year: this.formBuilder.control('', this.yearValidator),
});
}

yearValidator(control) {
if (control.value.trim().length === 0) {
return null;
}
let year = parseInt(control.value);
let minYear = 1800;
let maxYear = 2500;
if (year >= minYear && year <= maxYear) {
return null;
} else {
return {
'year': {
min: minYear,
max: maxYear
}
};
}
}

onSubmit(mediaItem) {
this.mediaItemService.add(mediaItem)
.subscribe(() => {
this.router.navigate(['/', mediaItem.medium]);
});
}
}
Loading