docs(displaying-data): convert Dart chapter to match newish TypeScript version

closes #666
This commit is contained in:
Kathy Walrath 2016-01-07 15:11:44 -08:00
parent 1365d82527
commit 096b99f2ae
10 changed files with 251 additions and 496 deletions

View File

@ -1,11 +1,7 @@
// #docplaster // #docregion
// #docregion final
library displaying_data.app_component;
// #docregion imports
import 'package:angular2/angular2.dart'; import 'package:angular2/angular2.dart';
// #enddocregion imports
import 'package:displaying_data/hero.dart'; import 'hero.dart';
final List<Hero> _heroes = [ final List<Hero> _heroes = [
new Hero(1, 'Windstorm'), new Hero(1, 'Windstorm'),
@ -25,19 +21,12 @@ final List<Hero> _heroes = [
{{ hero.name }} {{ hero.name }}
</li> </li>
</ul> </ul>
<!--#docregion message--> // #docregion message
<p *ngIf="heroes.length > 3">There are many heroes!</p> <p *ngIf="heroes.length > 3">There are many heroes!</p>
<!--#enddocregion message--> // #enddocregion message
''') ''')
class AppComponent { class AppComponent {
String title = 'Tour of Heroes'; String title = 'Tour of Heroes';
List<Hero> heroes = _heroes; List<Hero> heroes = _heroes;
Hero myHero = _heroes[0]; Hero myHero = _heroes[0];
} }
//#enddocregion final
/*
// #docregion final
bootstrap(AppComponent);
//#enddocregion final
*/

View File

@ -1,6 +1,4 @@
// #docregion // #docregion
library displaying_data.app_component_1;
import 'package:angular2/angular2.dart'; import 'package:angular2/angular2.dart';
@Component( @Component(

View File

@ -1,10 +1,14 @@
// #docregion // #docregion
library displaying_data.app_component_2;
// #docregion imports
import 'package:angular2/angular2.dart'; import 'package:angular2/angular2.dart';
// #enddocregion imports // #docregion mock-heroes
const List<String> _heroes = const [
'Windstorm',
'Bombasto',
'Magneta',
'Tornado'
];
// #enddocregion mock-heroes
@Component( @Component(
selector: 'my-app', selector: 'my-app',
@ -14,22 +18,15 @@ import 'package:angular2/angular2.dart';
<h2>My favorite hero is: {{myHero}}</h2> <h2>My favorite hero is: {{myHero}}</h2>
<p>Heroes:</p> <p>Heroes:</p>
<ul> <ul>
<!-- docregion li-repeater --> // #docregion li-repeater
<li *ngFor="#hero of heroes"> <li *ngFor="#hero of heroes">
{{ hero }} {{ hero }}
</li> </li>
<!-- enddocregion li-repeater --> // #enddocregion li-repeater
</ul>''' </ul>'''
// #enddocregion template // #enddocregion template
) )
// #docregion mock-heroes // #docregion mock-heroes
const List<String> _heroes = const [
'Windstorm',
'Bombasto',
'Magneta',
'Tornado'
];
class AppComponent { class AppComponent {
String title = 'Tour of Heroes'; String title = 'Tour of Heroes';
List<String> heroes = _heroes; List<String> heroes = _heroes;

View File

@ -1,13 +1,15 @@
// #docregion // #docregion
library displaying_data.app_component_3;
// #docregion imports
import 'package:angular2/angular2.dart'; import 'package:angular2/angular2.dart';
// #enddocregion imports // #docregion heroes
// #docregion import-hero import 'hero.dart';
import 'package:displaying_data/hero.dart';
// #enddocregion import-hero final List<Hero> _heroes = [
new Hero(1, 'Windstorm'),
new Hero(13, 'Bombasto'),
new Hero(15, 'Magneta'),
new Hero(20, 'Tornado')
];
// #enddocregion heroes
@Component( @Component(
selector: 'my-app', selector: 'my-app',
@ -24,19 +26,10 @@ import 'package:displaying_data/hero.dart';
// #enddocregion template // #enddocregion template
) )
// #docregion heroes // #docregion heroes
final List<Hero> _heroes = [
new Hero(1, 'Windstorm'),
new Hero(13, 'Bombasto'),
new Hero(15, 'Magneta'),
new Hero(20, 'Tornado')
];
// #enddocregion heroes
// #docregion class
class AppComponent { class AppComponent {
String title = 'Tour of Heroes'; String title = 'Tour of Heroes';
List<Hero> heroes = _heroes; List<Hero> heroes = _heroes;
Hero myHero = _heroes[0]; Hero myHero = _heroes[0];
} }
// #enddocregion class // #enddocregion heroes
// #enddocregion // #enddocregion

View File

@ -1,21 +0,0 @@
// #docregion
library displaying_data.app_ctor_component;
import 'package:angular2/angular2.dart';
@Component(
selector: 'my-app-ctor',
template: '''
<h1>{{title}} [Ctor version]</h1>
<h2>My favorite hero is: {{myHero}}</h2>''')
// #docregion app-ctor
class AppCtorComponent {
String title;
String myHero;
AppCtorComponent() {
this.title = 'Tour of Heroes';
this.myHero = 'Windstorm';
}
}
// #enddocregion app-ctor

View File

@ -1,12 +1,9 @@
// #docregion // #docregion
library displaying_data.hero;
class Hero { class Hero {
// #docregion id-parameter int id;
int number;
// #enddocregion id-parameter
String name; String name;
Hero(this.number, this.name); Hero(this.id, this.name);
String toString() => '$id: $name';
} }
// #enddocregion // #enddocregion

View File

@ -1,11 +1,15 @@
# #docregion
name: displaying_data name: displaying_data
description: Displaying Data Example description: Displaying Data Example
version: 0.0.1 version: 0.0.1
environment:
sdk: '>=1.13.0 <2.0.0'
dependencies: dependencies:
angular2: 2.0.0-beta.0 angular2: 2.0.0-beta.3
browser: ^0.10.0 browser: ^0.10.0
dart_to_js_script_rewriter: ^0.1.0
transformers: transformers:
- angular2: - angular2:
platform_directives: 'package:angular2/src/common/directives.dart#CORE_DIRECTIVES' platform_directives: 'package:angular2/common.dart#CORE_DIRECTIVES'
entry_points: web/main.dart entry_points: web/main.dart
- dart_to_js_script_rewriter

View File

@ -8,7 +8,7 @@
</head> </head>
<body> <body>
<!-- #docregion my-app --> <!-- #docregion my-app -->
<my-app></my-app> <my-app>Loading...</my-app>
<!-- #enddocregion my-app --> <!-- #enddocregion my-app -->
</body> </body>
</html> </html>

View File

@ -1,14 +1,20 @@
// #docregion // #docplaster
// #docregion final
import 'package:angular2/bootstrap.dart'; import 'package:angular2/bootstrap.dart';
// #enddocregion final
//import 'package:displaying_data/app_component_1.dart' as v1; //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_2.dart' as v2;
//import 'package:displaying_data/app_component_3.dart' as v3; //import 'package:displaying_data/app_component_3.dart' as v3;
// #docregion final
import 'package:displaying_data/app_component.dart'; import 'package:displaying_data/app_component.dart';
main() { main() {
// #enddocregion final
// pick one // pick one
// bootstrap(v1.AppComponent); // bootstrap(v1.AppComponent);
// bootstrap(v2.AppComponent); // bootstrap(v2.AppComponent);
// bootstrap(v3.AppComponent); // bootstrap(v3.AppComponent);
// #docregion final
bootstrap(AppComponent); bootstrap(AppComponent);
} }
// #enddocregion final

View File

@ -1,472 +1,264 @@
.l-main-section include ../../../../_includes/_util-fns
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:
figure.image-display :marked
img(src='/resources/images/examples/displaying-data-example1.png' alt="Example of Todo App") We typically display data in Angular by binding controls in an HTML template
to properties of an Angular component.
.l-main-section In this chapter, we'll create a component with a list of heroes. Each hero has a name.
h2#section-create-an-entry-point Create entry points and pubspec We'll display the list of hero names and
conditionally show a selected hero in a detail area below the list.
p. The final UI looks like this:
Open your favorite editor and create a directory with
a <code>web/main.dart</code> file,
a <code>web/index.html</code> file, and
a <code>pubspec.yaml</code> file:
code-tabs figure.image-display
code-pane(language="dart" name="web/main.dart" format="linenums"). img(src="/resources/images/devguide/displaying-data/final.png" alt="Final UI")
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").
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Displaying Data&lt;/title&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot;&gt;
&lt;script async src=&quot;main.dart&quot; type=&quot;application/dart&quot;&gt;&lt;/script&gt;
&lt;script async src=&quot;packages/browser/dart.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;display&gt;&lt;/display&gt;
&lt;/body&gt;
&lt;/html&gt;
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 <code>main.dart</code>.
The import of <code>show_properties.dart</code>
lets you implement part of the app in a different Dart file.
You've previously seen <code>angular2.dart</code> imported,
but that's unnecessary here because this version of <code>main.dart</code>
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.
<a id="interpolation"></a> <a id="interpolation"></a>
.l-main-section .l-main-section
h2#section-showing-properties-with-interpolation Showing properties with interpolation :marked
p. ## Showing component properties with interpolation
The simple method for binding text into templates is through interpolation, The easiest way to display a component property
where you put the name of a property is to bind the property name through interpolation.
inside <strong ng-non-bindable="">{{ }}</strong>. With interpolation, we put the property name in the view template, enclosed in double curly braces: `{{myHero}}`.
p. Let's build a small illustrative example together.
To see this working, first create a <code>lib</code> directory.
Under it, put a Dart file named <code>show_properties.dart</code>
that contains the following code:
code-example(language="dart" format="linenums" escape="html"). <a id="platform_directives"></a>
// lib/show_properties.dart Create a new project folder (`displaying`) and create 3 files:
library displaying_data.show_properties; `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') :marked
@View(template: ''' All of this code should look familiar from the
<p>My name: {{ myName }}</p> [QuickStart](../quickstart.html),
''') except for the `platform_directives` entry in `pubspec.yaml`
class DisplayComponent { and the imports in `main.dart`.
String myName = 'Alice';
}
p. In `pubspec.yaml`, the `platform_directives` entry lets us use
You've just defined a component that encompasses a view and controller for the app. The view core directives, such as the NgFor directive that we'll soon add to our app.
defines a template:
code-example(language="html" escape="html"). In `main.dart`, importing `app_component.dart` lets us implement part
<p>My name: {{ myName }}</p> 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. So that the code can run,
Angular will automatically pull the value of <code>myName</code> and let's create a stub for the `<my-app>` component.
insert it into the browser,
automatically updating it whenever it changes.
.callout.is-helpful Create a new directory called `lib`.
header Note In it, put a file called `app_component.dart`
p. with the following code:
While you've used <code>template:</code> to specify an inline view, for larger templates you'd
want to move them to a separate file and load them with <code>templateUrl:</code> instead.
p. +makeExample('displaying-data/dart/lib/app_component_1.dart', null, 'lib/app_component.dart')
One thing to notice is that although you've written
your <code>DisplayComponent</code> class, you haven't
used <code>new</code> to instantiate it.
Because your class is associated with <code>&lt;display&gt;</code> elements in
the DOM, Angular automatically calls <code>new</code> on
<code>DisplayComponent</code> and bind its properties to
that part of the template.
p. :marked
When you're building templates, data bindings like these have access to We defined a component with two properties: `title` and `myHero`.
the same scope of The template displays the two component properties using double curly brace
properties as your controller class does. interpolation:
Here your class is <code>DisplayComponent</code>, which has
just one property, <code>myName</code>.
p. +makeExample('displaying-data/dart/lib/app_component_1.dart', 'template')(format=".")
Add a second line to the template,
so you can see Angular dynamically update content:
code-example(language="html"). :marked
&lt;p&gt;Current time: {{ time }}&lt;/p&gt; 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 <code>dart:async</code> so you can use a Timer,
and give the <code>DisplayComponent</code> a starting value for time and
create a periodic Timer call to update the time:
code-example(language="dart"). .l-sub-section
import 'dart:async'; :marked
... More precisely, the redisplay occurs after some kind of asynchronous event related to
class DisplayComponent { the view such as a keystroke, a timer completion, or an async `XHR` response.
String myName = 'Alice'; We don't have those in this sample.
String time; But then the properties aren't changing on their own either. For the moment we must operate on faith.
Timer _timer; :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() { Notice the CSS `selector` in the `@Component` decorator that specifies an element named "my-app".
_updateTime(null); Remember back in QuickStart that we added the `<my-app>` element to the body of our `index.html` file:
_timer = new Timer.periodic(new Duration(seconds: 1), _updateTime); +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 `<my-app>`
in the `index.html`, finds it, instantiates an instance of `AppComponent`, and renders it
inside the `<my-app>` tag.
<a id="ng-for"></a> 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.
<a id="ngFor"></a>
.l-main-section .l-main-section
h2#Create-an-array Display an iterable using *ng-for :marked
p Moving up from a single value, create a property that's a list of values. ## Showing a list property with NgFor
code-example(language="dart"). 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.
class DisplayComponent { +makeExample('displaying-data/dart/lib/app_component_2.dart', 'mock-heroes', 'lib/app_component.dart (excerpt)')(format=".")
String myName = 'Alice';
List&lt;String&gt; friendNames = const [
'Aarav',
'Martín',
'Shannon',
'Ariana',
'Kai'
];
}
p. :marked
You can then use this list in your template with Now we use the Angular `NgFor` "repeater" directive in the template to display
the <code>ng-for</code> directive to create copies of DOM elements each item in the `heroes` list.
with one for each item in the list.
code-example(language="dart"). +makeExample('displaying-data/dart/lib/app_component_2.dart', 'template','lib/app_component.dart (excerpt)')(format=".")
@View(template: '''
&lt;p&gt;My name: {{ myName }}&lt;/p&gt;
&lt;p&gt;Friends:&lt;/p&gt;
&lt;ul&gt;
&lt;li <span class="pnk">*ng-for="#name of friendNames"</span>&gt;
{{ name }}
&lt;/li&gt;
&lt;/ul&gt;
''', <span class="pnk">directives: const [NgFor]</span>)
p.
To make <code>ng-for</code> work,
you need to add the Angular <code>NgFor</code> directive,
so that Angular knows to include it.
Add <code>NgFor</code> using the optional <code>directives</code> parameter.
p Reload and you've got your list of friends! :marked
p. Our presentation is the familiar HTML unordered list with `<ul>` and `<li>` tags. Let's focus on the `<li>` tag.
Again, Angular will mirror changes you make to this list over in the DOM. +makeExample('displaying-data/dart/lib/app_component_2.dart', 'li-repeater')(format=".")
Add a new item and it appears in your list.
Delete one and Angular deletes the &lt;li&gt;.
Reorder items and Angular makes the corresponding reorder of the DOM list.
p Let's look at the few lines that do the work again: :marked
We added a somewhat mysterious `*ngFor` to the `<li>` element.
That's the Angular "repeater" directive.
Its presence on the `<li>` tag marks that `<li>` element (and its children) as the "repeater template".
code-example(language="html"). .alert.is-important
&lt;li *ng-for=&quot;#name of friendNames&quot;&gt; :marked
{{ name }} Don't forget the leading asterisk (\*) in `*ngFor`.
&lt;/li&gt; <!-- Learn more about this and `NgFor` in the [Template Syntax](./template-syntax.html#ngFor) chapter.-->
p The way to read this is: :marked
ul Notice the `#hero` in the `NgFor` double-quoted instruction.
li. The `#hero` is a local template variable
<code>*ng-for</code>: Create a DOM element for each item in an <!-- TODO: link to (./template-syntax.html#local-vars) -->
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols">iterable</a> declaration.
such as a list. The `#` prefix declares a local variable name named `hero`.
li <code>#name</code>: Refer to individual values of the iterable as <code>name</code>.
li <code>of friendNames</code>: The iterable to use is called <code>friendNames</code> in the current controller.
p Using this syntax, you can build UI lists from any iterable object. Angular duplicates the `<li>` 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 .l-main-section
h2#Create-a-class Create a model and inject it :marked
p. ## Creating a class for the data
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.
p. We are defining our data directly inside our component.
Make a <code>FriendsService</code> class to implement a model That's fine for a demo but certainly isn't a best practice. It's not even a good practice.
containing a list of friends. Although we won't do anything about that in this chapter, we'll make a mental note to fix this down the road.
Put this in a new file under <code>lib/</code>
named <code>friends_service.dart</code>. Here's what the class looks like:
code-example(language="dart" format="linenums"). At the moment, we're binding to a list of strings. We do that occasionally in real applications, but
// lib/friends_service.dart most of the time we're binding to more specialized objects.
library displaying_data.friends_service;
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() Create a new file in the `lib/` folder called `hero.dart` with the following code.
class FriendsService { +makeExample('displaying-data/dart/lib/hero.dart',null,'lib/hero.dart')
List&lt;String&gt; names = ['Aarav', 'Martín', 'Shannon', 'Ariana', 'Kai'];
}
p. :marked
Now you can replace the current list of friends in DisplayComponent. We've defined a class with a constructor, a string description, and two properties: `id` and `name`.
Import <code>friends_service.dart</code>,
and add a FriendsService parameter to the constructor.
Then set <code>friendNames</code> to the names provided by the service.
code-example(language="dart").
// In lib/show_properties.dart
...
<span class="pnk">import 'package:displaying_data/friends_service.dart';</span>
...
class DisplayComponent {
String myName = 'Alice';
List&lt;String&gt; friendNames;
DisplayComponent(<span class="pnk">FriendsService friendsService</span>) {
<span class="pnk">friendNames = friendsService.names;</span>
}
}
p.
Next, make FriendsService available to dependency injection
by adding a <code>viewProviders</code> parameter to DisplayComponent's
<code>@Component</code> annotation:
code-example(language="dart").
@Component(selector: 'display', <span class="pnk">viewProviders: const [FriendsService]</span>)
<a id="ng-if"></a>
.l-main-section .l-main-section
h2#Conditionally-displaying-data-with-NgIf Conditionally display data using *ng-if :marked
p. ## Using the Hero class
Lastly, before we move on, let's handle showing parts of our UI conditionally with <code>*ng-if</code>. The Let's make the `heroes` property in our component return a list of these Hero objects.
<code>NgIf</code> directive adds or removes elements from the DOM based on the expression you provide. - 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").
&lt;p *ng-if="friendNames.length &gt; 3"&gt;You have many friends!&lt;/p&gt;
p. :marked
Also add <code>NgIf</code> to the list of directives, Our display looks the same, but now we know much better what a hero really is.
so Angular knows to include it:
code-example(language="dart"). <a id="ngIf"></a>
directives: const[NgFor, <span class="pnk">NgIf</span>]
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: '''
&lt;p&gt;My name: {{ myName }}&lt;/p&gt;
&lt;p&gt;Friends:&lt;/p&gt;
&lt;ul&gt;
&lt;li *ng-for="#name of friendNames"&gt;
{{ name }}
&lt;/li&gt;
&lt;/ul&gt;
&lt;p *ng-if="friendNames.length &gt; 3"&gt;You have many friends!&lt;/p&gt;
''', directives: const [NgFor, NgIf])
class DisplayComponent {
String myName = 'Alice';
List&lt;String&gt; 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&lt;String&gt; 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").
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Displaying Data&lt;/title&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot;&gt;
&lt;script async src=&quot;main.dart&quot; type=&quot;application/dart&quot;&gt;&lt;/script&gt;
&lt;script async src=&quot;packages/browser/dart.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;display&gt;&lt;/display&gt;
&lt;/body&gt;
&lt;/html&gt;
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 .l-main-section
h2#section-explanations Explanations :marked
## Conditional display with NgIf
.l-sub-section Sometimes the app should display a view or a portion of a view only under specific circumstances.
h3 Using multiple Dart files in an Angular app
p. In our example, we'd like to display a message if we have a large number of heroes &mdash; say, more than 3.
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.
p. The Angular `NgIf` directive inserts or removes an element based on a boolean condition.
To use the API defined in <code>show_properties.dart</code>, We can see it in action by adding the following paragraph at the bottom of the template:
<code>main.dart</code> must import that file. +makeExample('displaying-data/dart/lib/app_component.dart', 'message')
The import statement uses the package name .alert.is-important
(defined in <code>pubspec.yaml</code> to be <b>displaying_data</b>) :marked
and the path to <code>show_properties.dart</code> Don't forget the leading asterisk (\*) in `*ngIf`.
(starting at the app's top directory, <!-- Learn more about this and `NgIf` in the [Template Syntax](./template-syntax.html#ngIf) chapter. -->
but omitting the <code>lib/</code> 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 <code>show_properties.dart</code> specifies for its library
is similar to the path used to import the library,
but with no ".dart" suffix and
with dots (<code>.</code>) instead of slashes (<code>/</code>).
<a href="https://www.dartlang.org/articles/style-guide/#do-name-libraries-and-source-files-using-lowercasewithunderscores">Naming
conventions for libraries</a>,
along with lots of other helpful information, are in the
<a href="https://www.dartlang.org/articles/style-guide/">Dart Style Guide</a>.
p.
Another import lets <code>show_properties.dart</code>
use the API defined in <code>friends_service.dart</code>:
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 <code>show_properties.dart</code> and <code>friends_service.dart</code>
are under <code>lib</code>,
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—<code>main.dart</code>—imports
<code>bootstrap.dart</code> so that it can call <code>bootstrap()</code>.
Both <code>show_properties.dart</code> and <code>friends_service.dart</code>
import <code>angular2.dart</code>
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()
...
p. :marked
For more information on implementing Dart libraries, see The template expression
<a href="https://www.dartlang.org/docs/dart-up-and-running/ch02.html#libraries-and-visibility">Libraries and visibility</a> <!--PENDING: link to (./template-syntax.html#template-expressions)-->
in the inside the double quotes looks much like Dart, and it _is_ much like Dart.
<a href="https://www.dartlang.org/docs/dart-up-and-running/ch02.html">Dart language tour</a>. 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')