diff --git a/public/docs/_examples/displaying-data/ts/src/app/app-ctor.ts b/public/docs/_examples/displaying-data/ts/app/app-ctor.component.ts similarity index 86% rename from public/docs/_examples/displaying-data/ts/src/app/app-ctor.ts rename to public/docs/_examples/displaying-data/ts/app/app-ctor.component.ts index d257de577c..3b17bcb6e8 100644 --- a/public/docs/_examples/displaying-data/ts/src/app/app-ctor.ts +++ b/public/docs/_examples/displaying-data/ts/app/app-ctor.component.ts @@ -1,4 +1,4 @@ -import {Component, bootstrap} from 'angular2/angular2'; +import {Component} from 'angular2/core'; @Component({ selector: 'my-app-ctor', diff --git a/public/docs/_examples/displaying-data/ts/src/app/app.1.ts b/public/docs/_examples/displaying-data/ts/app/app.component.1.ts similarity index 77% rename from public/docs/_examples/displaying-data/ts/src/app/app.1.ts rename to public/docs/_examples/displaying-data/ts/app/app.component.1.ts index a12c6f44e6..7b1d5b8cca 100644 --- a/public/docs/_examples/displaying-data/ts/src/app/app.1.ts +++ b/public/docs/_examples/displaying-data/ts/app/app.component.1.ts @@ -1,5 +1,5 @@ // #docregion -import {Component, bootstrap} from 'angular2/angular2'; +import {Component} from 'angular2/core'; @Component({ selector: 'my-app', @@ -14,5 +14,3 @@ export class AppComponent { title = 'Tour of Heroes'; myHero = 'Windstorm'; } - -bootstrap(AppComponent); diff --git a/public/docs/_examples/displaying-data/ts/src/app/app.2.ts b/public/docs/_examples/displaying-data/ts/app/app.component.2.ts similarity index 69% rename from public/docs/_examples/displaying-data/ts/src/app/app.2.ts rename to public/docs/_examples/displaying-data/ts/app/app.component.2.ts index bdefa492da..ed30ee0ebc 100644 --- a/public/docs/_examples/displaying-data/ts/src/app/app.2.ts +++ b/public/docs/_examples/displaying-data/ts/app/app.component.2.ts @@ -1,7 +1,5 @@ // #docregion -// #docregion imports -import {Component, bootstrap, NgFor} from 'angular2/angular2'; -// #enddocregion imports +import {Component} from 'angular2/core'; @Component({ selector: 'my-app', @@ -12,16 +10,13 @@ import {Component, bootstrap, NgFor} from 'angular2/angular2';

Heroes:

- `, + ` // #enddocregion template - // #docregion directives - directives: [NgFor] - // #enddocregion directives }) // #docregion mock-heroes export class AppComponent { diff --git a/public/docs/_examples/displaying-data/ts/src/app/app.3.ts b/public/docs/_examples/displaying-data/ts/app/app.component.3.ts similarity index 67% rename from public/docs/_examples/displaying-data/ts/src/app/app.3.ts rename to public/docs/_examples/displaying-data/ts/app/app.component.3.ts index 327882bd11..79edbfdfae 100644 --- a/public/docs/_examples/displaying-data/ts/src/app/app.3.ts +++ b/public/docs/_examples/displaying-data/ts/app/app.component.3.ts @@ -1,7 +1,5 @@ // #docregion -// #docregion import-ng-if -import {Component, bootstrap, NgFor, NgIf} from 'angular2/angular2'; -// #enddocregion import-ng-if +import {Component} from 'angular2/core'; // #docregion import-hero import {Hero} from './hero'; // #enddocregion import-hero @@ -11,18 +9,15 @@ import {Hero} from './hero'; // #docregion template template: `

{{title}}

-

My favorite hero is: {{myHero}}

+

My favorite hero is: {{myHero.name}}

Heroes:

- `, + ` // #enddocregion template - // #docregion directives - directives: [NgFor, NgIf] - // #enddocregion directives }) // #docregion class export class AppComponent { diff --git a/public/docs/_examples/displaying-data/ts/src/app/app.final.ts b/public/docs/_examples/displaying-data/ts/app/app.component.ts similarity index 61% rename from public/docs/_examples/displaying-data/ts/src/app/app.final.ts rename to public/docs/_examples/displaying-data/ts/app/app.component.ts index a2a7ddee8d..a85a09081e 100644 --- a/public/docs/_examples/displaying-data/ts/src/app/app.final.ts +++ b/public/docs/_examples/displaying-data/ts/app/app.component.ts @@ -1,7 +1,7 @@ // #docplaster // #docregion final // #docregion imports -import {Component, bootstrap, CORE_DIRECTIVES} from 'angular2/angular2'; +import {Component} from 'angular2/core'; // #enddocregion imports import {Hero} from './hero' @@ -12,17 +12,14 @@ import {Hero} from './hero'

My favorite hero is: {{myHero.name}}

Heroes:

// #docregion message -

There are many heroes!

+

There are many heroes!

// #enddocregion message -`, - // #docregion directives - directives: [CORE_DIRECTIVES] - // #enddocregion directives +` }) export class AppComponent { @@ -35,10 +32,3 @@ export class AppComponent { ]; myHero = this.heroes[0]; } -//#enddocregion final -/* -// #docregion final - -bootstrap(AppComponent); -//#enddocregion final -*/ \ No newline at end of file diff --git a/public/docs/_examples/displaying-data/ts/app/boot.1.ts b/public/docs/_examples/displaying-data/ts/app/boot.1.ts new file mode 100644 index 0000000000..511180507b --- /dev/null +++ b/public/docs/_examples/displaying-data/ts/app/boot.1.ts @@ -0,0 +1,16 @@ +import {bootstrap} from 'angular2/platform/browser'; +import {AppCtorComponent} from './app-ctor.component'; +import {AppComponent as v1} from './app.component.1'; +import {AppComponent as v2} from './app.component.2'; +import {AppComponent as v3} from './app.component.3'; + +import {AppComponent as final} from './app.component'; + +// pick one +//bootstrap(v1); +//bootstrap(v2); +//bootstrap(v3); +bootstrap(final); + +// for doc testing +bootstrap(AppCtorComponent); \ No newline at end of file diff --git a/public/docs/_examples/displaying-data/ts/app/boot.ts b/public/docs/_examples/displaying-data/ts/app/boot.ts new file mode 100644 index 0000000000..ec4c9d12d6 --- /dev/null +++ b/public/docs/_examples/displaying-data/ts/app/boot.ts @@ -0,0 +1,5 @@ +// #docregion +import {bootstrap} from 'angular2/platform/browser'; +import {AppComponent} from './app.component'; + +bootstrap(AppComponent); diff --git a/public/docs/_examples/displaying-data/ts/src/app/hero.ts b/public/docs/_examples/displaying-data/ts/app/hero.ts similarity index 100% rename from public/docs/_examples/displaying-data/ts/src/app/hero.ts rename to public/docs/_examples/displaying-data/ts/app/hero.ts diff --git a/public/docs/_examples/displaying-data/ts/src/index.html b/public/docs/_examples/displaying-data/ts/index.1.html similarity index 54% rename from public/docs/_examples/displaying-data/ts/src/index.html rename to public/docs/_examples/displaying-data/ts/index.1.html index 8323a99413..b76c23e201 100644 --- a/public/docs/_examples/displaying-data/ts/src/index.html +++ b/public/docs/_examples/displaying-data/ts/index.1.html @@ -3,20 +3,18 @@ Displaying Data - - + + - loading... -
loading... diff --git a/public/docs/_examples/displaying-data/ts/index.html b/public/docs/_examples/displaying-data/ts/index.html new file mode 100644 index 0000000000..a1b14f30e1 --- /dev/null +++ b/public/docs/_examples/displaying-data/ts/index.html @@ -0,0 +1,22 @@ + + + + + Displaying Data + + + + + + + + loading... + + + + diff --git a/public/docs/_examples/displaying-data/ts/package.json b/public/docs/_examples/displaying-data/ts/package.json index d82952aa81..9d1da95ef5 100644 --- a/public/docs/_examples/displaying-data/ts/package.json +++ b/public/docs/_examples/displaying-data/ts/package.json @@ -1,21 +1,15 @@ { - "name": "angular2-getting-started", + "name": "angular2-displaying-data", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { - "tsc": "tsc -p src -w", - "start": "live-server --open=src" + "tsc": "tsc", + "tsc:w": "tsc -w", + "lite": "lite-server", + "both": "concurrent \"npm run tsc:w\" \"npm run lite\" " }, "keywords": [], "author": "", - "license": "ISC", - "dependencies": { - "angular2": "2.0.0-alpha.46", - "systemjs": "0.19.2" - }, - "devDependencies": { - "live-server": "^0.8.1", - "typescript": "^1.6.2" - } + "license": "ISC" } diff --git a/public/docs/_examples/displaying-data/ts/plnkr.json b/public/docs/_examples/displaying-data/ts/plnkr.json new file mode 100644 index 0000000000..ee037aec0b --- /dev/null +++ b/public/docs/_examples/displaying-data/ts/plnkr.json @@ -0,0 +1,10 @@ +{ + "description": "Displaying Data", + "files": [ + "!**/*.d.ts", + "!**/*.js", + "!**/app-ctor.component.ts", + "!**/*.[1,2,3].*" + ], + "tags": ["Template"] +} \ No newline at end of file diff --git a/public/docs/_examples/displaying-data/ts/src/app/bootstrap.ts b/public/docs/_examples/displaying-data/ts/src/app/bootstrap.ts deleted file mode 100644 index 2c459c04b6..0000000000 --- a/public/docs/_examples/displaying-data/ts/src/app/bootstrap.ts +++ /dev/null @@ -1,16 +0,0 @@ -import {bootstrap} from 'angular2/angular2'; -import {AppCtorComponent} from './app-ctor'; -import {AppComponent as v1} from './app.1'; -import {AppComponent as v2} from './app.2'; -import {AppComponent as v3} from './app.3'; - -import {AppComponent as final} from './app.final'; - -// pick one -//bootstrap(v1); -//bootstrap(v2); -//bootstrap(v3); -bootstrap(final); - -// for doc testing -bootstrap(AppCtorComponent); \ No newline at end of file diff --git a/public/docs/_examples/displaying-data/ts/src/plnkr.json b/public/docs/_examples/displaying-data/ts/src/plnkr.json deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/public/docs/_examples/displaying-data/ts/src/tsconfig.json b/public/docs/_examples/displaying-data/ts/tsconfig.json similarity index 50% rename from public/docs/_examples/displaying-data/ts/src/tsconfig.json rename to public/docs/_examples/displaying-data/ts/tsconfig.json index 6a58b35a58..6ffae9f106 100644 --- a/public/docs/_examples/displaying-data/ts/src/tsconfig.json +++ b/public/docs/_examples/displaying-data/ts/tsconfig.json @@ -1,11 +1,16 @@ { "compilerOptions": { "target": "ES5", - "module": "commonjs", + "module": "system", + "moduleResolution": "node", "sourceMap": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "removeComments": false, - "noImplicitAny": false - } + "noImplicitAny": true, + "suppressImplicitAnyIndexErrors": true + }, + "exclude": [ + "node_modules" + ] } \ No newline at end of file diff --git a/public/docs/ts/latest/guide/displaying-data.jade b/public/docs/ts/latest/guide/displaying-data.jade index 0601f04115..ce56593599 100644 --- a/public/docs/ts/latest/guide/displaying-data.jade +++ b/public/docs/ts/latest/guide/displaying-data.jade @@ -11,7 +11,10 @@ include ../../../../_includes/_util-fns In this chapter, we'll create a component with a list of heroes. Each hero has a name. We'll display the list of hero names and conditionally show a selected hero in a detail area below the list. - Our UI looks like this: + + [Live Example](/resources/live-examples/displaying-data/ts/src/plnkr.html). + + Our final UI looks like this: figure.image-display img(src="/resources/images/devguide/displaying-data/final.png" alt="Final UI") @@ -22,16 +25,16 @@ figure.image-display ## Showing component properties with interpolation The easiest way to display a component property is to bind the property name through interpolation. - With interpolation, we put the property name in the view template, enclosed in double curly braces: {‌{myHero}} . + With interpolation, we put the property name in the view template, enclosed in double curly braces: `{{myHero}}`. Let's build a small illustrative example together. Create a new project folder (`displaying-data`) and follow the steps in the [QuickStart](../quickstart.html). - Then modify the `app.ts` file by changing the template and the body of the component. + Then modify the `app.component.ts` file by changing the template and the body of the component. When we're done, it should look like this: -+makeExample('displaying-data/ts/src/app/app.1.ts') ++makeExample('displaying-data/ts/app/app.component.1.ts', null, 'app/app.component.ts') :marked We added two properties to the formerly empty component: `title` and `myHero`. @@ -39,7 +42,7 @@ figure.image-display Our revised template displays the two component properties using the double curly brace interpolation: -+makeExample('displaying-data/ts/src/app/app.1.ts', 'template') ++makeExample('displaying-data/ts/app/app.component.1.ts', 'template')(format=".") .l-sub-section :marked The template is a multi-line string within ECMAScript 2015 back-tics (\`). @@ -64,13 +67,24 @@ figure.image-display Notice the CSS `selector` in the `@Component` decorator that specifies an element named "my-app". Remember back in QuickStart that we added the `` element to the body of our `index.html` -+makeExample('displaying-data/ts/src/index.html', 'my-app') ++makeExample('displaying-data/ts/index.html', 'my-app')(format=".") :marked - When we bootstrap with the `AppComponent` class (see the bottom of `app.ts`), Angular looks for a `` + When we bootstrap with the `AppComponent` class (see `boot.ts`), Angular looks for a `` in the `index.html`, finds it, instantiates an instance of `AppComponent`, and renders it inside the `` tag. - + + We're ready to see changes in a running app by firing up the npm script that both compiles and serves our applications + while watching for changes. +code-example(format=""). + npm run both +:marked + We should see the title and hero name: +figure.image-display + img(src="/resources/images/devguide/displaying-data/title-and-hero.png" alt="Title and Hero") +:marked + Let's review some of the choices we made and consider alternatives. + ## Template inline or template file? We can store our component's template in one of two places. @@ -89,41 +103,41 @@ figure.image-display This is a wonderfully concise and compact technique. Some folks prefer to declare the properties and initialize them within a constructor like this: -+makeExample('displaying-data/ts/src/app/app-ctor.ts', 'app-ctor') ++makeExample('displaying-data/ts/app/app-ctor.component.ts', 'app-ctor')(format=".") :marked That's fine too. The choice between them is a matter of taste and organization policy. We'll adopt the more terse "variable assignment" style in this chapter simply because there will be less code to read. - + .l-main-section :marked ## Showing an array property with NgFor We want to display a list of heroes. We begin by adding a mock heroes name array to the component, just above `myHero` and redefine `myHero` to be the first name in the array. -+makeExample('displaying-data/ts/src/app/app.2.ts', 'mock-heroes') ++makeExample('displaying-data/ts/app/app.component.2.ts', 'mock-heroes', 'app/app.component.ts (class)')(format=".") :marked Now we use the Angular `NgFor` "repeater" Directive in the template to display each item in the `heroes` list. -+makeExample('displaying-data/ts/src/app/app.2.ts', 'template') ++makeExample('displaying-data/ts/app/app.component.2.ts', 'template','app/app.component.ts (template)')(format=".") :marked Our presentation is the familiar HTML unordered list with `
    ` and `
  • ` tags. Let's focus on the `
  • ` tag. -+makeExample('displaying-data/ts/src/app/app.2.ts', 'li-repeater') ++makeExample('displaying-data/ts/app/app.component.2.ts', 'li-repeater')(format=".") :marked - We added a somewhat mysterious `*ng-for` to the `
  • ` element. + We added a somewhat mysterious `*ngFor` to the `
  • ` element. That's the Angular "repeater" directive. It's presence on the `
  • ` tag marks that `
  • ` element (and its children) as the "repeater template". .alert.is-important :marked - Don't forget the leading asterisk (\*) in front of `*ng-for`. It is an essential part of the syntax. - Learn more about this and `NgFor` in the [Template Syntax](./template-syntax.html#ng-for) chapter. + Don't forget the leading asterisk (\*) in front of `*ngFor`. It is an essential part of the syntax. + Learn more about this and `NgFor` in the [Template Syntax](./template-syntax.html#ngFor) chapter. :marked Notice the `#hero` in the `NgFor` double-quoted instruction. @@ -140,25 +154,8 @@ figure.image-display In fact, `NgFor` can repeat items for any [iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols) object. :marked - ## Register the NgFor Directive - - Angular doesn't know that this template uses the `NgFor` directive. - Our application will not run right now. Angular will complain that it doesn't know what `NgFor` is. - - We have to register the `NgFor` directive with the component metadata by making two changes to the app.ts file. - - First, we import the `NgFor` symbol from the Angular library by extending the existing `import` statement. - Look for it in the following: -+makeExample('displaying-data/ts/src/app/app.2.ts', 'imports') - -:marked - Second, we register `NgFor` as a directive accessible to the template by updating the - `@Component` decorator with a `directives` array property whose only item is `NgFor`: - -+makeExample('displaying-data/ts/src/app/app.2.ts', 'directives') - -:marked - Now the heroes will appear in the view as an unordered list. + Assuming we're still running under the `npm both` command, + we should see heroes appearing in an unordered list. figure.image-display img(src="/resources/images/devguide/displaying-data/hero-names-list.png" alt="After ngfor") @@ -169,15 +166,15 @@ figure.image-display We are defining our data directly inside our component. That's fine for a demo but certainly isn't a best practice. It's not even a good practice. - We won't do anything about that in this chapter. + Although we won't do anything about that in this chapter, we'll make a mental note to fix this down the road. At the moment, we're binding to an array of strings. We do that occasionally in real applications but most of the time we're displaying objects, potentially instances of classes. Let's turn our array of hero names into an array of `Hero` objects. For that we'll need a `Hero' class. - Create a new file called `hero.ts` and add the following short snippet to it. -+makeExample('displaying-data/ts/src/app/hero.ts') + Create a new file in the `app/` folder called `hero.ts` with the following short bit of code. ++makeExample('displaying-data/ts/app/hero.ts', null, 'app/hero.ts')(format = ".") :marked We've defined a class with a constructor and two properties: `id` and `name`. @@ -186,7 +183,7 @@ figure.image-display advantage of a TypeScript short-cut in our declaration of the constructor parameters. Consider the first parameter: -+makeExample('displaying-data/ts/src/app/hero.ts', 'id-parameter') ++makeExample('displaying-data/ts/app/hero.ts', 'id-parameter') :marked That brief syntax simultaneously @@ -199,18 +196,18 @@ figure.image-display ## Use the Hero class Let's redefine the heroes property in our component to return an array of these Heroes and also set the `myHero` property with the first of these mock heroes. -+makeExample('displaying-data/ts/src/app/app.3.ts', 'heroes') ++makeExample('displaying-data/ts/app/app.component.3.ts', 'heroes', 'app.component.ts (excerpt)')(format=".") :marked We'll have to update the template. At the moment it displays the entire hero object which used to be a string value. Let's fix that so we interpolate the `hero.name` property -+makeExample('displaying-data/ts/src/app/app.3.ts', 'template') ++makeExample('displaying-data/ts/app/app.component.3.ts', 'template','app.component.ts (template)')(format=".") :marked Our display looks the same but we know how much better it is under the hood. - + .l-main-section :marked ## Conditional display with NgIf @@ -221,11 +218,11 @@ figure.image-display The Angular `NgIf` directive will insert or remove an element based on a truthy/falsey condition. We can see it in action by adding the following paragraph at the bottom of the template: -+makeExample('displaying-data/ts/src/app/app.final.ts', 'message') ++makeExample('displaying-data/ts/app/app.component.ts', 'message') .alert.is-important :marked - Don't forget the leading asterisk (\*) in front of `*ng-if`. It is an essential part of the syntax. - Learn more about this and `NgIf` in the [Template Syntax](./template-syntax.html#ng-if) chapter. + Don't forget the leading asterisk (\*) in front of `*ngIf`. It is an essential part of the syntax. + Learn more about this and `NgIf` in the [Template Syntax](./template-syntax.html#ngIf) chapter. :marked The [template expression](./template-syntax.html#template-expressions) inside the double quotes @@ -239,42 +236,12 @@ figure.image-display That hardly matters here. It would matter a great deal from a performance perspective if we were conditionally including or excluding a big chunk of HTML with many data bindings. -:marked - As with the `NgFor`, we must add the `NgIf` directive to the component's metadata. - - We should extend our `import` statement as before ... -+makeExample('displaying-data/ts/src/app/app.3.ts', 'import-ng-if') - -:marked - ... and add it to the directives array: -+makeExample('displaying-data/ts/src/app/app.3.ts', 'directives') - :marked Try it out. We have four items in the array so the message should appear. - Delete one of the elements from the array, refresh the browser, and the message should no longer appear. - -.l-main-section -:marked - ## Use the CORE_DIRECTIVES Constant - - There are other core Angular directives, such as `NgClass` and `NgSwitch`, - that we often use in our apps. - Extending the `import` statement and adding to the `directives` array for each one gets old. - - Fortunately, Angular provides a constant array called `CORE_DIRECTIVES` - that includes many of the directives that we use all the time. - - Let's simplify our lives, discard the `NgFor` and `NgIf`, use the constant for all of them. - - We'll revise our `import` statement one last time. -+makeExample('displaying-data/ts/src/app/app.final.ts', 'imports') - -:marked - and update the `directives` metadata -+makeExample('displaying-data/ts/src/app/app.final.ts', 'directives') - -:marked - Pro tip: we register this constant in almost every template we write. + Go back into `app.component.ts` and delete or comment out one of the elements from the hero array. + The browser should refresh automatically and the message should disappear. + + Play with it. .l-main-section :marked @@ -284,14 +251,14 @@ figure.image-display - use **`NgFor`** to display a list of items, - use a TypeScript class to shape the model data for our component and display properties of that model, - use **`NgIf`** to conditionally display a chunk of HTML based on a boolean expression. - - register common component directives with **`CORE_DIRECTIVES` constant** Our final code: -+makeTabs('displaying-data/ts/src/app/app.final.ts, '+ - 'displaying-data/ts/src/app/hero.ts', - 'final,', - 'app.ts, hero.ts') ++makeTabs(`displaying-data/ts/app/app.component.ts, + displaying-data/ts/app/hero.ts, + displaying-data/ts/app/boot.ts`, + 'final,,', + 'app/app.component.ts, app/hero.ts, boot.ts') .l-main-section :marked diff --git a/public/resources/images/devguide/displaying-data/title-and-hero.png b/public/resources/images/devguide/displaying-data/title-and-hero.png new file mode 100644 index 0000000000..72cab782f1 Binary files /dev/null and b/public/resources/images/devguide/displaying-data/title-and-hero.png differ