diff --git a/public/docs/_examples/displaying-data/dart/lib/app_component.dart b/public/docs/_examples/displaying-data/dart/lib/app_component.dart index 82f84d163e..5374ba81a8 100644 --- a/public/docs/_examples/displaying-data/dart/lib/app_component.dart +++ b/public/docs/_examples/displaying-data/dart/lib/app_component.dart @@ -1,11 +1,7 @@ -// #docplaster -// #docregion final -library displaying_data.app_component; - -// #docregion imports +// #docregion import 'package:angular2/angular2.dart'; -// #enddocregion imports -import 'package:displaying_data/hero.dart'; + +import 'hero.dart'; final List _heroes = [ new Hero(1, 'Windstorm'), @@ -25,19 +21,12 @@ final List _heroes = [ {{ hero.name }} - +// #docregion message

There are many heroes!

- +// #enddocregion message ''') class AppComponent { String title = 'Tour of Heroes'; List heroes = _heroes; Hero myHero = _heroes[0]; } -//#enddocregion final -/* -// #docregion final - -bootstrap(AppComponent); -//#enddocregion final -*/ diff --git a/public/docs/_examples/displaying-data/dart/lib/app_component_1.dart b/public/docs/_examples/displaying-data/dart/lib/app_component_1.dart index 3643da7564..1991a6645c 100644 --- a/public/docs/_examples/displaying-data/dart/lib/app_component_1.dart +++ b/public/docs/_examples/displaying-data/dart/lib/app_component_1.dart @@ -1,6 +1,4 @@ // #docregion -library displaying_data.app_component_1; - import 'package:angular2/angular2.dart'; @Component( diff --git a/public/docs/_examples/displaying-data/dart/lib/app_component_2.dart b/public/docs/_examples/displaying-data/dart/lib/app_component_2.dart index 5c3b3bc633..ff97982532 100644 --- a/public/docs/_examples/displaying-data/dart/lib/app_component_2.dart +++ b/public/docs/_examples/displaying-data/dart/lib/app_component_2.dart @@ -1,10 +1,14 @@ // #docregion -library displaying_data.app_component_2; - -// #docregion imports import 'package:angular2/angular2.dart'; -// #enddocregion imports +// #docregion mock-heroes +const List _heroes = const [ + 'Windstorm', + 'Bombasto', + 'Magneta', + 'Tornado' +]; +// #enddocregion mock-heroes @Component( selector: 'my-app', @@ -14,22 +18,15 @@ import 'package:angular2/angular2.dart';

My favorite hero is: {{myHero}}

Heroes:

    - +// #docregion li-repeater
  • {{ hero }}
  • - +// #enddocregion li-repeater
''' // #enddocregion template ) // #docregion mock-heroes -const List _heroes = const [ - 'Windstorm', - 'Bombasto', - 'Magneta', - 'Tornado' -]; - class AppComponent { String title = 'Tour of Heroes'; List heroes = _heroes; diff --git a/public/docs/_examples/displaying-data/dart/lib/app_component_3.dart b/public/docs/_examples/displaying-data/dart/lib/app_component_3.dart index c8c31fce60..9a39808d18 100644 --- a/public/docs/_examples/displaying-data/dart/lib/app_component_3.dart +++ b/public/docs/_examples/displaying-data/dart/lib/app_component_3.dart @@ -1,13 +1,15 @@ // #docregion -library displaying_data.app_component_3; - -// #docregion imports import 'package:angular2/angular2.dart'; -// #enddocregion imports -// #docregion import-hero -import 'package:displaying_data/hero.dart'; +// #docregion heroes +import 'hero.dart'; -// #enddocregion import-hero +final List _heroes = [ + new Hero(1, 'Windstorm'), + new Hero(13, 'Bombasto'), + new Hero(15, 'Magneta'), + new Hero(20, 'Tornado') +]; +// #enddocregion heroes @Component( selector: 'my-app', @@ -24,19 +26,10 @@ import 'package:displaying_data/hero.dart'; // #enddocregion template ) // #docregion heroes -final List _heroes = [ - new Hero(1, 'Windstorm'), - new Hero(13, 'Bombasto'), - new Hero(15, 'Magneta'), - new Hero(20, 'Tornado') -]; - -// #enddocregion heroes -// #docregion class class AppComponent { String title = 'Tour of Heroes'; List heroes = _heroes; Hero myHero = _heroes[0]; } -// #enddocregion class +// #enddocregion heroes // #enddocregion diff --git a/public/docs/_examples/displaying-data/dart/lib/app_ctor_component.dart b/public/docs/_examples/displaying-data/dart/lib/app_ctor_component.dart deleted file mode 100644 index b1d0ba336d..0000000000 --- a/public/docs/_examples/displaying-data/dart/lib/app_ctor_component.dart +++ /dev/null @@ -1,21 +0,0 @@ -// #docregion -library displaying_data.app_ctor_component; - -import 'package:angular2/angular2.dart'; - -@Component( - selector: 'my-app-ctor', - template: ''' -

{{title}} [Ctor version]

-

My favorite hero is: {{myHero}}

''') -// #docregion app-ctor -class AppCtorComponent { - String title; - String myHero; - - AppCtorComponent() { - this.title = 'Tour of Heroes'; - this.myHero = 'Windstorm'; - } -} -// #enddocregion app-ctor diff --git a/public/docs/_examples/displaying-data/dart/lib/hero.dart b/public/docs/_examples/displaying-data/dart/lib/hero.dart index 3fb139db73..6542e4f4ca 100644 --- a/public/docs/_examples/displaying-data/dart/lib/hero.dart +++ b/public/docs/_examples/displaying-data/dart/lib/hero.dart @@ -1,12 +1,9 @@ // #docregion -library displaying_data.hero; - class Hero { - // #docregion id-parameter - int number; - // #enddocregion id-parameter + int id; String name; - Hero(this.number, this.name); + Hero(this.id, this.name); + String toString() => '$id: $name'; } // #enddocregion diff --git a/public/docs/_examples/displaying-data/dart/pubspec.yaml b/public/docs/_examples/displaying-data/dart/pubspec.yaml index b79474da42..4d08d84b88 100644 --- a/public/docs/_examples/displaying-data/dart/pubspec.yaml +++ b/public/docs/_examples/displaying-data/dart/pubspec.yaml @@ -1,11 +1,15 @@ +# #docregion name: displaying_data description: Displaying Data Example version: 0.0.1 +environment: + sdk: '>=1.13.0 <2.0.0' dependencies: - angular2: 2.0.0-beta.0 + angular2: 2.0.0-beta.3 browser: ^0.10.0 + dart_to_js_script_rewriter: ^0.1.0 transformers: - angular2: - platform_directives: 'package:angular2/src/common/directives.dart#CORE_DIRECTIVES' + platform_directives: 'package:angular2/common.dart#CORE_DIRECTIVES' entry_points: web/main.dart - +- dart_to_js_script_rewriter diff --git a/public/docs/_examples/displaying-data/dart/web/index.html b/public/docs/_examples/displaying-data/dart/web/index.html index 6efa48834c..4e74743dae 100644 --- a/public/docs/_examples/displaying-data/dart/web/index.html +++ b/public/docs/_examples/displaying-data/dart/web/index.html @@ -8,7 +8,7 @@ - + Loading... diff --git a/public/docs/_examples/displaying-data/dart/web/main.dart b/public/docs/_examples/displaying-data/dart/web/main.dart index 73837a7180..69650f0740 100644 --- a/public/docs/_examples/displaying-data/dart/web/main.dart +++ b/public/docs/_examples/displaying-data/dart/web/main.dart @@ -1,14 +1,20 @@ -// #docregion +// #docplaster +// #docregion final import 'package:angular2/bootstrap.dart'; +// #enddocregion final //import 'package:displaying_data/app_component_1.dart' as v1; //import 'package:displaying_data/app_component_2.dart' as v2; //import 'package:displaying_data/app_component_3.dart' as v3; +// #docregion final import 'package:displaying_data/app_component.dart'; main() { +// #enddocregion final // pick one // bootstrap(v1.AppComponent); // bootstrap(v2.AppComponent); // bootstrap(v3.AppComponent); +// #docregion final bootstrap(AppComponent); } +// #enddocregion final diff --git a/public/docs/dart/latest/guide/displaying-data.jade b/public/docs/dart/latest/guide/displaying-data.jade index 60cdc5f5c0..2ce2bce80d 100644 --- a/public/docs/dart/latest/guide/displaying-data.jade +++ b/public/docs/dart/latest/guide/displaying-data.jade @@ -1,472 +1,264 @@ -.l-main-section - p. - Let's walk through how to display a property and a list of properties, - and then to conditionally show content - based on state. The final UI looks like this: +include ../../../../_includes/_util-fns - figure.image-display - img(src='/resources/images/examples/displaying-data-example1.png' alt="Example of Todo App") +:marked + We typically display data in Angular by binding controls in an HTML template + to properties of an Angular component. -.l-main-section - h2#section-create-an-entry-point Create entry points and pubspec + 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. - p. - Open your favorite editor and create a directory with - a web/main.dart file, - a web/index.html file, and - a pubspec.yaml file: + The final UI looks like this: - code-tabs - code-pane(language="dart" name="web/main.dart" format="linenums"). - import 'package:angular2/bootstrap.dart'; - import 'package:displaying_data/show_properties.dart'; +figure.image-display + img(src="/resources/images/devguide/displaying-data/final.png" alt="Final UI") - main() { - bootstrap(DisplayComponent); - } - code-pane(language="html" name="web/index.html" format="linenums"). - <!DOCTYPE html> - <html> - <head> - <title>Displaying Data</title> - <link rel="stylesheet" href="style.css"> - <script async src="main.dart" type="application/dart"></script> - <script async src="packages/browser/dart.js"></script> - </head> - <body> - <display></display> - </body> - </html> - code-pane(language="yaml" name="pubspec.yaml" format="linenums"). - name: displaying_data - description: Dart version of Angular 2 example, Displaying Data - version: 0.0.1 - dependencies: - angular2: 2.0.0-alpha.47 - browser: ^0.10.0 - transformers: - - angular2: - entry_points: web/main.dart - p. - All of this code should look familiar from the previous page, - except for the imports in main.dart. - The import of show_properties.dart - lets you implement part of the app in a different Dart file. - You've previously seen angular2.dart imported, - but that's unnecessary here because this version of main.dart - doesn't implement any components or other injectable types. - - p. - All three of these files remain similar in the rest of the examples, - so we'll focus on what changes. .l-main-section - h2#section-showing-properties-with-interpolation Showing properties with interpolation - p. - The simple method for binding text into templates is through interpolation, - where you put the name of a property - inside {{ }}. +:marked + ## 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}}`. - p. - To see this working, first create a lib directory. - Under it, put a Dart file named show_properties.dart - that contains the following code: + Let's build a small illustrative example together. - code-example(language="dart" format="linenums" escape="html"). - // lib/show_properties.dart - library displaying_data.show_properties; + + Create a new project folder (`displaying`) and create 3 files: + `pubspec.yaml`, `web/index.html`, and `web/main.dart`. + Put these contents in the files: - import 'package:angular2/angular2.dart'; +- var stylePattern = [{ otl: /(platform_directives.*$)/gm }, null, null]; ++makeTabs('displaying-data/dart/pubspec.yaml, displaying-data/dart/web/index.html, displaying-data/dart/web/main.dart', ',,final', 'pubspec.yaml, web/index.html, web/main.dart', stylePattern) - @Component(selector: 'display') - @View(template: ''' -

My name: {{ myName }}

- ''') - class DisplayComponent { - String myName = 'Alice'; - } +:marked + All of this code should look familiar from the + [QuickStart](../quickstart.html), + except for the `platform_directives` entry in `pubspec.yaml` + and the imports in `main.dart`. - p. - You've just defined a component that encompasses a view and controller for the app. The view - defines a template: + In `pubspec.yaml`, the `platform_directives` entry lets us use + core directives, such as the NgFor directive that we'll soon add to our app. - code-example(language="html" escape="html"). -

My name: {{ myName }}

+ In `main.dart`, importing `app_component.dart` lets us implement part + of the app in a different Dart file. The QuickStart version of `main.dart` + imported `angular2.dart`, but we don't need that import here because + this version of `main.dart` is so basic: it only bootstraps the app, + and doesn't implement any components or other injectable types. - p. - Angular will automatically pull the value of myName and - insert it into the browser, - automatically updating it whenever it changes. + So that the code can run, + let's create a stub for the `` component. - .callout.is-helpful - header Note - p. - While you've used template: to specify an inline view, for larger templates you'd - want to move them to a separate file and load them with templateUrl: instead. + Create a new directory called `lib`. + In it, put a file called `app_component.dart` + with the following code: - p. - One thing to notice is that although you've written - your DisplayComponent class, you haven't - used new to instantiate it. - Because your class is associated with <display> elements in - the DOM, Angular automatically calls new on - DisplayComponent and bind its properties to - that part of the template. ++makeExample('displaying-data/dart/lib/app_component_1.dart', null, 'lib/app_component.dart') - p. - When you're building templates, data bindings like these have access to - the same scope of - properties as your controller class does. - Here your class is DisplayComponent, which has - just one property, myName. +:marked + We defined a component with two properties: `title` and `myHero`. + The template displays the two component properties using double curly brace + interpolation: - p. - Add a second line to the template, - so you can see Angular dynamically update content: ++makeExample('displaying-data/dart/lib/app_component_1.dart', 'template')(format=".") - code-example(language="html"). - <p>Current time: {{ time }}</p> +:marked + Angular automatically pulls the value of the `title` and `myHero` properties from the component and + inserts those values into the browser. Angular updates the display + when these properties change. - p. - Then import dart:async so you can use a Timer, - and give the DisplayComponent a starting value for time and - create a periodic Timer call to update the time: - code-example(language="dart"). - import 'dart:async'; - ... - class DisplayComponent { - String myName = 'Alice'; - String time; - Timer _timer; +.l-sub-section + :marked + More precisely, the redisplay occurs after some kind of asynchronous event related to + the view such as a keystroke, a timer completion, or an async `XHR` response. + We don't have those in this sample. + But then the properties aren't changing on their own either. For the moment we must operate on faith. +:marked + Notice that we haven't called **new** to create an instance of the `AppComponent` class. + Angular is creating an instance for us. How? - DisplayComponent() { - _updateTime(null); - _timer = new Timer.periodic(new Duration(seconds: 1), _updateTime); - } + 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` file: ++makeExample('displaying-data/dart/web/index.html', 'my-app')(format=".") - _updateTime(Timer _) { - time = new DateTime.now().toString(); - } - } - p Reload the app, and you'll now see the seconds updating automatically. +:marked + When we bootstrap with the `AppComponent` class (in `main.dart`), Angular looks for a `` + in the `index.html`, finds it, instantiates an instance of `AppComponent`, and renders it + inside the `` tag. - + Try running the app. It should display the title and hero name: +figure.image-display + img(src="/resources/images/devguide/displaying-data/title-and-hero.png" alt="Title and Hero") +// TODO: Here the TS version says "Let's review some of the choices we made and consider alternatives." However, it's unclear where this review ends. Clarify the structure in the TS, and make sure this is the best place for the Dart version of this section. + +#performance.l-sub-section + :marked + ### Template inline or template file? + + We can store our component's template in one of two places. + We can define it *inline* using the `template` property, as we do here. + Or we can define the template in a separate HTML file and link to it in + the component metadata using the `@Component` decorator's `templateUrl` property. + + The choice between inline and separate HTML is a matter of taste, + circumstances, and organization policy. + Here we're using inline HTML because the template is small, and the demo + is simpler without the additional HTML file. + + In either style, the template data bindings have the same access to the component's properties. + + + .l-main-section - h2#Create-an-array Display an iterable using *ng-for - p Moving up from a single value, create a property that's a list of values. +:marked + ## Showing a list property with NgFor - code-example(language="dart"). - class DisplayComponent { - String myName = 'Alice'; - List<String> friendNames = const [ - 'Aarav', - 'Martín', - 'Shannon', - 'Ariana', - 'Kai' - ]; - } + We want to display a list of heroes. We begin by adding a list of hero names to the component and redefine `myHero` to be the first name in the list. ++makeExample('displaying-data/dart/lib/app_component_2.dart', 'mock-heroes', 'lib/app_component.dart (excerpt)')(format=".") - p. - You can then use this list in your template with - the ng-for directive to create copies of DOM elements - with one for each item in the list. +:marked + Now we use the Angular `NgFor` "repeater" directive in the template to display + each item in the `heroes` list. - code-example(language="dart"). - @View(template: ''' - <p>My name: {{ myName }}</p> - <p>Friends:</p> - <ul> - <li *ng-for="#name of friendNames"> - {{ name }} - </li> - </ul> - ''', directives: const [NgFor]) ++makeExample('displaying-data/dart/lib/app_component_2.dart', 'template','lib/app_component.dart (excerpt)')(format=".") - p. - To make ng-for work, - you need to add the Angular NgFor directive, - so that Angular knows to include it. - Add NgFor using the optional directives parameter. - p Reload and you've got your list of friends! - p. - Again, Angular will mirror changes you make to this list over in the DOM. - Add a new item and it appears in your list. - Delete one and Angular deletes the <li>. - Reorder items and Angular makes the corresponding reorder of the DOM list. +:marked + Our presentation is the familiar HTML unordered list with `
    ` and `
  • ` tags. Let's focus on the `
  • ` tag. ++makeExample('displaying-data/dart/lib/app_component_2.dart', 'li-repeater')(format=".") - p Let's look at the few lines that do the work again: +:marked + We added a somewhat mysterious `*ngFor` to the `
  • ` element. + That's the Angular "repeater" directive. + Its presence on the `
  • ` tag marks that `
  • ` element (and its children) as the "repeater template". - code-example(language="html"). - <li *ng-for="#name of friendNames"> - {{ name }} - </li> +.alert.is-important + :marked + Don't forget the leading asterisk (\*) in `*ngFor`. + - p The way to read this is: - ul - li. - *ng-for: Create a DOM element for each item in an - iterable - such as a list. - li #name: Refer to individual values of the iterable as name. - li of friendNames: The iterable to use is called friendNames in the current controller. +:marked + Notice the `#hero` in the `NgFor` double-quoted instruction. + The `#hero` is a local template variable + + declaration. + The `#` prefix declares a local variable name named `hero`. - p Using this syntax, you can build UI lists from any iterable object. + Angular duplicates the `
  • ` for each item in the list, setting the `hero` variable + to the item (the hero) in the current iteration. Angular uses that variable as the + context for the interpolation in the double curly braces. + +.l-sub-section + :marked + We happened to give `NgFor` a list to display. + In fact, `NgFor` can repeat items for any [Iterable](https://api.dartlang.org/stable/dart-core/Iterable-class.html) object. + +:marked + Assuming we're still running under the `npm go` 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") + +.callout.is-important + header Did the app break? + :marked + If the app stops working after adding `*ngFor`, + make sure `pubspec.yaml` has the [correct **platform_directives** entry](#platform_directives). + A missing or incorrect `platform_directives` entry results in template parse errors. .l-main-section - h2#Create-a-class Create a model and inject it - p. - Before we get too much further, we should mention that - putting the model (list) directly into the controller isn't proper form. - We should separate the concerns by having another class - serve the role of model and inject it into the controller. +:marked + ## Creating a class for the data - p. - Make a FriendsService class to implement a model - containing a list of friends. - Put this in a new file under lib/ - named friends_service.dart. Here's what the class looks like: + 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. + Although we won't do anything about that in this chapter, we'll make a mental note to fix this down the road. - code-example(language="dart" format="linenums"). - // lib/friends_service.dart - library displaying_data.friends_service; + At the moment, we're binding to a list of strings. We do that occasionally in real applications, but + most of the time we're binding to more specialized objects. - import 'package:angular2/angular2.dart'; + Let's turn our list of hero names into a list of `Hero` objects. For that we'll need a `Hero` class. - @Injectable() - class FriendsService { - List<String> names = ['Aarav', 'Martín', 'Shannon', 'Ariana', 'Kai']; - } + Create a new file in the `lib/` folder called `hero.dart` with the following code. ++makeExample('displaying-data/dart/lib/hero.dart',null,'lib/hero.dart') - p. - Now you can replace the current list of friends in DisplayComponent. - Import friends_service.dart, - and add a FriendsService parameter to the constructor. - Then set friendNames to the names provided by the service. +:marked + We've defined a class with a constructor, a string description, and two properties: `id` and `name`. - code-example(language="dart"). - // In lib/show_properties.dart - ... - import 'package:displaying_data/friends_service.dart'; - ... - class DisplayComponent { - String myName = 'Alice'; - List<String> friendNames; - - DisplayComponent(FriendsService friendsService) { - friendNames = friendsService.names; - } - } - - p. - Next, make FriendsService available to dependency injection - by adding a viewProviders parameter to DisplayComponent's - @Component annotation: - - code-example(language="dart"). - @Component(selector: 'display', viewProviders: const [FriendsService]) - - .l-main-section - h2#Conditionally-displaying-data-with-NgIf Conditionally display data using *ng-if - p. - Lastly, before we move on, let's handle showing parts of our UI conditionally with *ng-if. The - NgIf directive adds or removes elements from the DOM based on the expression you provide. +:marked + ## Using the Hero class + Let's make the `heroes` property in our component return a list of these Hero objects. +- var stylePattern = { otl: /(import.*$)|(final)|(new Hero.*$)/gm }; ++makeExample('displaying-data/dart/lib/app_component_3.dart', 'heroes', 'app_component.dart (excerpt)', stylePattern)(format=".") - p See it in action by adding a paragraph at the end of your template: +:marked + We'll have to update the template. + At the moment it displays the string value of the `Hero` object. + Let's fix that so we display only the hero's `name` property. +- var stylePattern = { otl: /(myHero\.name)|(hero\.name)/gm }; ++makeExample('displaying-data/dart/lib/app_component_3.dart', 'template','app_component.dart (template)', stylePattern)(format=".") - code-example(language="html"). - <p *ng-if="friendNames.length > 3">You have many friends!</p> - p. - Also add NgIf to the list of directives, - so Angular knows to include it: +:marked + Our display looks the same, but now we know much better what a hero really is. - code-example(language="dart"). - directives: const[NgFor, NgIf] - p. - The list currently has 5 items, so if you run the app you'll see the message - congratulating you on your many friends. - Remove two items from the list, reload your browser, - and see that the message no longer displays. - - p Here's the final code. - - code-tabs - code-pane(language="dart" name="lib/show_properties.dart" format="linenums"). - library displaying_data.show_properties; - - import 'package:angular2/angular2.dart'; - import 'package:displaying_data/friends_service.dart'; - - @Component(selector: 'display', viewProviders: const [FriendsService]) - @View(template: ''' - <p>My name: {{ myName }}</p> - <p>Friends:</p> - <ul> - <li *ng-for="#name of friendNames"> - {{ name }} - </li> - </ul> - <p *ng-if="friendNames.length > 3">You have many friends!</p> - ''', directives: const [NgFor, NgIf]) - class DisplayComponent { - String myName = 'Alice'; - List<String> friendNames; - - DisplayComponent(FriendsService friendsService) { - friendNames = friendsService.names; - } - } - code-pane(language="dart" name="lib/friends_service.dart" format="linenums"). - library displaying_data.friends_service; - - import 'package:angular2/angular2.dart'; - - @Injectable() - class FriendsService { - List<String> names = ['Aarav', 'Martín', 'Shannon', 'Ariana', 'Kai']; - } - code-pane(language="dart" name="web/main.dart" format="linenums"). - import 'package:angular2/bootstrap.dart'; - import 'package:displaying_data/show_properties.dart'; - - main() { - bootstrap(DisplayComponent); - } - code-pane(language="html" name="web/index.html" format="linenums"). - <!DOCTYPE html> - <html> - <head> - <title>Displaying Data</title> - <link rel="stylesheet" href="style.css"> - <script async src="main.dart" type="application/dart"></script> - <script async src="packages/browser/dart.js"></script> - </head> - <body> - <display></display> - </body> - </html> - code-pane(language="yaml" name="pubspec.yaml" format="linenums"). - name: displaying_data - description: Displaying Data example - version: 0.0.1 - dependencies: - angular2: 2.0.0-alpha.47 - browser: ^0.10.0 - transformers: - - angular2: - entry_points: web/main.dart + .l-main-section - h2#section-explanations Explanations +:marked + ## Conditional display with NgIf - .l-sub-section - h3 Using multiple Dart files in an Angular app + Sometimes the app should display a view or a portion of a view only under specific circumstances. - p. - Dart offers a few ways to implement an app in multiple files. - In this guide, each example is in a single package, - and each Dart file implements a separate library. - For a bigger project, you might split the code into libraries - in two or more packages. + In our example, we'd like to display a message if we have a large number of heroes — say, more than 3. - p. - To use the API defined in show_properties.dart, - main.dart must import that file. - The import statement uses the package name - (defined in pubspec.yaml to be displaying_data) - and the path to show_properties.dart - (starting at the app's top directory, - but omitting the lib/ directory). - - code-example(language="dart"). - // In web/main.dart: - ... - import 'package:displaying_data/show_properties.dart'; - ... - - // In lib/show_properties.dart: - library displaying_data.show_properties; - ... - - p. - The name that show_properties.dart specifies for its library - is similar to the path used to import the library, - but with no ".dart" suffix and - with dots (.) instead of slashes (/). - Naming - conventions for libraries, - along with lots of other helpful information, are in the - Dart Style Guide. - - p. - Another import lets show_properties.dart - use the API defined in friends_service.dart: - - code-example(language="dart"). - // In lib/show_properties.dart: - library displaying_data.show_properties; - ... - import 'package:displaying_data/friends_service.dart'; - ... - - // In lib/friends_service.dart: - library displaying_data.friends_service; - ... - - p. - Because both show_properties.dart and friends_service.dart - are under lib, - the import could instead use a relative path: - - code-example(language="dart"). - // In lib/show_properties.dart: - library displaying_data.show_properties; - ... - import 'friends_service.dart'; - ... - - // In lib/friends_service.dart: - library displaying_data.friends_service; - ... - - p. - The app's entry point—main.dart—imports - bootstrap.dart so that it can call bootstrap(). - Both show_properties.dart and friends_service.dart - import angular2.dart - so that they can define Angular components and models. - - code-example(language="dart"). - // In web/main.dart: - import 'package:angular2/bootstrap.dart'; - ... - bootstrap(...) - ... - - // In lib/show_properties.dart: - ... - import 'package:angular2/angular2.dart'; - ... - @Component(...) - @View(...) - ... - - // In lib/friends_service.dart: - ... - import 'package:angular2/angular2.dart'; - ... - @Injectable() - ... + The Angular `NgIf` directive inserts or removes an element based on a boolean condition. + We can see it in action by adding the following paragraph at the bottom of the template: ++makeExample('displaying-data/dart/lib/app_component.dart', 'message') +.alert.is-important + :marked + Don't forget the leading asterisk (\*) in `*ngIf`. + - p. - For more information on implementing Dart libraries, see - Libraries and visibility - in the - Dart language tour. +:marked + The template expression + + inside the double quotes looks much like Dart, and it _is_ much like Dart. + When the component's list of heroes has more than 3 items, Angular adds the paragraph to the DOM and the message appears. + If there are 3 or fewer items, Angular omits the paragraph, so no message appears. +.alert.is-helpful + :marked + Angular isn't showing and hiding the message. It is adding and removing the paragraph element from the DOM. + That hardly matters here. But 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 + Try it out. Because the list has four items, the message should appear. + Go back into `app_component.dart` and delete or comment out one of the elements from the hero list. + The browser should refresh automatically and the message should disappear. + +.l-main-section +:marked + ## Summary + Now we know how to use: + - **interpolation** with double curly braces to display a component property + - **`NgFor`** to display a list of items + - a Dart class to shape the **model data** for our component and display properties of that model + - **`NgIf`** to conditionally display a chunk of HTML based on a boolean expression + + + Here's our final code: + ++makeTabs(`displaying-data/dart/lib/app_component.dart, + displaying-data/dart/lib/hero.dart, + displaying-data/dart/pubspec.yaml, + displaying-data/dart/web/index.html, + displaying-data/dart/web/main.dart`, + ',,,,final', + 'lib/app_component.dart, lib/hero.dart, pubspec.yaml, web/index.html, web/main.dart')