Skip to content
/ ng4 Public

A small Angular 4 project for learning and fun

Notifications You must be signed in to change notification settings

chrisco/ng4

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ng4 - Small Angular 4 Projects for Learning and Fun

First App (app1)

Download Node.js, if necessary

I tried to upgrade to the latest version (7.7.4 with npm 4.1.2), but was then unable to install the Angular CLI (see below), so I downgraded to the LTS version (6.10.1 with npm 3.10.10). I was then able to install the CLI and create a new Angular app (see below). NOTE: I ran $ npm cache clean after each Node.js installation to make sure a package isn't loading anything from npm's cache. Not sure if that was necessary.

Install CLI tool and create new app

$ npm install -g @angular/cli
$ ng new app1 # 'app1' is the name of the new app

Build and serve it

$ ng serve

Visit http://localhost:4200 and you should see "app works!"

CLI Deep Dive & Troubleshooting

If you want to dive deeper into the CLI and learn more about its usage, have a look at its official documentation: https://github.com/angular/angular-cli/wiki

You encountered issues during the installation of the CLI or setup of a new Angular project?

A lot of problems are solved by making sure you're using the latest version of NodeJS, npm and the CLI itself.

Updating Node.js

Go to http://nodejs.org and download the latest version. May be good/necessary to uninstall older versions first (Google it).

Updating npm

$ npm install -g npm

(sudo may be required on Mac/Linux)

Updating the CLI

$ npm uninstall -g angular-cli @angular/cli
$ npm cache clean
$ npm install -g @angular/cli

Some common issues & solutions

  1. EADDR error (Address already in use). You might already have another ng serve process running - make sure to quit that or use $ ng serve --port ANOTHERPORT to serve your project on a new port

  2. Changes not reflected in browser (App is not compiling). Check if the window running $ ng serve displays an error. If that's not the case, make sure you're using the latest CLI version and try restarting your CLI

Install Bootstrap

From inside the app1 folder:

$ npm i -S bootstrap

Edit the .angular-cli.json file to include Bootstrap:

"../node_modules/bootstrap/dist/css/bootstrap.min.css",

How an Angular App Gets Loaded and Started

Video

Creating a New Component

Video

Understanding the Role of AppModule and Component Declaration

Video

Using Custom Components

Video

Creating Components with the CLI & Nesting Components

Video

Example: Generate servers component:

$ ng generate component servers

Same command using shorthand syntax:

$ ng g c servers

Formatting With Prettier

While I've used Sublime and experimented with WebStorm and VS Code, I'm currently using Atom. To format code, I'm currently using the prettier-atom package. Ctrl + Alt + F

Fully Understanding the Component Selector

You can select by element (e.g., <app-servers></app-servers>), attribute (e.g. <div app-servers></div>), or class (e.g., <div class="app-servers"></div>). You can't select by id. Typically use elements with/for components.

Video

Practice Making Components (ass1)

  • Generated one with $ ng g c SuccessAlert and one by hand.
  • Used templateUrl and StyleUrls with one and template and styles (i.e., 'inline') with the other.
  • Used applicable Bootstrap alert classes. See this video for style-by-hand example.

Data Binding

Videos

  1. String Interpolation uses {{ someString }}
  2. Property Binding uses [someProperty]
  3. Event Binding uses (someEvent)
  4. Combine 'Property' and 'Event' binding with [(ngModel)]="someProperty"

Property Binding vs String Interpolation

Which Properties and Events can you bind to?

Pretty much all of them. Use console.log(foo) to see something properties and events.

Example: Binging a method to a click event:

<button class="btn btn-primary" (click)="doSomething()">Do Something</button>

Passing and Using Data with Event Binding

Video

Two-Way Data Binding

Video

For Two-Way-Binding to work, you need to enable the ngModel directive. This is done by adding the FormsModule to the imports[] array in the AppModule. You then also need to add the import from @angular/forms in the app.module.ts file:

import { FormsModule } from '@angular/forms';

Directives

Directives are instructions in the DOM (Components are a type of Directive with a template). Typically add directives with an Attribute selector (but can add with the other ways as well, if want/necessary).

Video

ngIf Example

Video

<p *ngIf="serverCreated">Server was created, server name is {{ serverName }}</p>

The * is required because ngIf is a Structural Directive, which means it changes the structure of the DOM (in this case, it either adds the Element or it doesn't).

ngIf else Example

Vide

<p *ngIf="serverCreated; else noServer">Server was created, server name is {{ serverName }}</p>
<ng-template #noServer>
  <p>Server has not been created yet</p>
</ng-template>

The noServer is called a 'Local Reference.'

ngStyle

Video

ngStyle is and Attribute Directive. Unlike Structural Directives, Attribute Directives don't add or remove elements. Instead, they change the element they are placed on.

ngClass

Video

ngFor

Video

Course Project (proj1)

A recipe book / shopping list app. See video clip.

Intro Videos

  1. Intro
  2. Planning
  3. Setup

Project Setup

$ ng new proj1
$ cd proj1
$ ng serve

Install Bootstrap

$ npm i -S bootstrap

Edit .angular-cli.json to add "../node_modules/bootstrap/dist/css/bootstrap.min.css", line to styles array. Edit app.component.html to 'Hello World' Bootstrap.

$ ng serve

Create Components

Video

  1. Create header component manually (for practice).
  2. Create all other components with the CLI:
$ ng g c recipes --spec false
$ ng g c recipes/recipe-list --spec false
$ ng g c recipes/recipe-list/recipe-item --spec false
$ ng g c recipes/recipe-detail --spec false
$ ng g c shopping-list --spec false
$ ng g c shopping-list/shopping-edit --spec false

Place Components

Video

Unix Command to Find and Remove Files

I used this to remove files ending in .orig after using resolving merge conflict:

$ find . -type f -name '*.orig' -delete
  1. Source (StackExchange Recursively delete all files with a given extension)
  2. More about the find command (CyberCiti.biz FAQ)

Configure Navbar

Video

Create Recipe Model

Video

Design RecipeItem

Video

Output List of Recipes With ngFor

Video

Design RecipeDetail

Video

Design ShoppingListComponent

Video

Create Ingredient Model

Video

Creating and Outputting the Shopping List

Video

Design ShoppingEditComponent

Video

Next Steps

Video

Debugging Videos

  1. Angular Error Messages
  2. Debugging Code in the Browser Using Sourcemaps
  3. Using Augury to Dive into Angular Apps

Components & Databinding Deep Dive

Practicing Property & Event Binding and View Encapsulation (ass4-cmps-dbnd)

Great practice. See video and commits for details ($ git log --grep="Ass 4").

6. Course Project, Continued (proj1)

Videos

7. Directives Deep Dive

Videos

$ ng g d better-highlight --spec false

8. Course Project - Directives

Building and Using a Dropdown Directive video

9. Using Services & Dependency Injection

Videos

Practice

10. Course Project - Services & Dependency Injection

Videos

11: Changing Pages with Routing

12: Course Project - Routing

13: Understanding Observables

14: Course Project - Observables

15: Handling Forms in Angular Apps

Setting Default Option Selection

HTML (standard way, I think, to dynamically populate the options):

<form [formGroup]="signupForm" (ngSubmit)="onSaveProject()">
  <div class="form-group">
    <label for="status">Project Status:</label>
    <select name="status" formControlName="status" [(ngModel)]="signupForm.statusChoices" class="form-control">
      <option *ngFor="let choice of statusChoices" [value]="choice">{{ choice }}</option>
    </select>
  </div>
</form>

TS (the setTimeout() is the thing that does the thing):

export class AppComponent implements OnInit {
  signupForm: FormGroup;
  statusChoices = ['Stable', 'Critical', 'Finished'];
  defaultStatus = 'Critical';

  ngOnInit() {
    this.signupForm = new FormGroup({
      projectname: new FormControl(
        null,
        [Validators.required, CustomValidators.invalidProjectName],
        CustomValidators.asyncInvalidProjectName,
      ),
      email: new FormControl(null, [Validators.required, Validators.email]),
      status: new FormControl(null),
    });

    setTimeout(() => {
      this.signupForm.controls['status'].patchValue(this.defaultStatus);
    }, 0);
  }

  onSaveProject() {
    console.log(this.signupForm);
  }
}

Source: how i can set default value ? #17

16: Course Project - Forms

18: Making Http Requests

19: Course Project - Http

20: Authentication & Route Protection in Angular Apps

$ npm install promise-polyfill --save-exact
$ npm i

21: Using Angular Modules & Optimizing Apps

You can add canActivate to the lazy loaded routes but that of course means, that you might load code which in the end can't get accessed anyways. It would be better to check that BEFORE loading the code.

You can enforce this behavior by adding the canLoad guard to the route which points to the lazily loaded module:

{ path: 'recipes', loadChildren: './recipes/recipes.module#RecipesModule', canLoad: [AuthGuard] }

In this example, the AuthGuard should implement the CanLoad interface.

22: Deploying an Angular App

23: Angular Animations

With the release of Angular 4, the general syntax of Angular Animations didn't change.

However, the animation functions were moved into their own package and you now also need to add a special module to your imports[] array in the AppModule.

Specifically, the following adjustments are required:

24: A Basic Introduction to Unit Testing in Angular Apps

25: Course Roundup

26: About the Course Update & Angular 4

27: Custom Project & Workflow Setup

28: Bonus: TypeScript Introduction (for Angular 2 Usage)

TODOs (besides general cleanup)

  • If user clicks New Recipe button when not logged in, do something (such as display message saying they must be signed in or redirect them to sign-in).

About

A small Angular 4 project for learning and fun

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published