Change all the For and If directives to NgFor and NgIf
This commit is contained in:
parent
253d29badf
commit
d61e884e08
|
@ -104,7 +104,7 @@
|
||||||
want to move them to a separate file and load them with <code>templateUrl:</code> instead.
|
want to move them to a separate file and load them with <code>templateUrl:</code> instead.
|
||||||
|
|
||||||
.l-main-section
|
.l-main-section
|
||||||
h2#Create-an-array Create an array property and use For on the view
|
h2#Create-an-array Create an array property and use NgFor on the view
|
||||||
p Moving up from a single property, create an array to display as a list.
|
p Moving up from a single property, create an array to display as a list.
|
||||||
|
|
||||||
code-tabs
|
code-tabs
|
||||||
|
@ -127,7 +127,7 @@
|
||||||
<span class='otl'>this.names = ["Aarav", "Martín", "Shannon", "Ariana", "Kai"];</span>
|
<span class='otl'>this.names = ["Aarav", "Martín", "Shannon", "Ariana", "Kai"];</span>
|
||||||
}
|
}
|
||||||
p.
|
p.
|
||||||
You can then use this array in your template with the <code>For</code> directive to create copies of DOM elements
|
You can then use this array in your template with the <code>NgFor</code> directive to create copies of DOM elements
|
||||||
with one for each item in the array.
|
with one for each item in the array.
|
||||||
|
|
||||||
code-tabs
|
code-tabs
|
||||||
|
@ -137,7 +137,7 @@
|
||||||
<p>My name: {{ myName }}</p>
|
<p>My name: {{ myName }}</p>
|
||||||
<p>Friends:</p>
|
<p>Friends:</p>
|
||||||
<ul>
|
<ul>
|
||||||
<li *for="#name of names">
|
<li *ng-for="#name of names">
|
||||||
{{ name }}
|
{{ name }}
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
@ -148,22 +148,22 @@
|
||||||
'<p>My name: {{ myName }}</p>' +
|
'<p>My name: {{ myName }}</p>' +
|
||||||
'<p>Friends:</p>' +
|
'<p>Friends:</p>' +
|
||||||
'<ul>' +
|
'<ul>' +
|
||||||
'<li *for="#name of names">' +
|
'<li *ng-for="#name of names">' +
|
||||||
'{{ name }}' +
|
'{{ name }}' +
|
||||||
'</li>' +
|
'</li>' +
|
||||||
'</ul>',
|
'</ul>',
|
||||||
|
|
||||||
p.
|
p.
|
||||||
To make this work, you'll also need to add the <code>For</code> directive used by the template so
|
To make this work, you'll also need to add the <code>NgFor</code> directive used by the template so
|
||||||
that Angular knows to include it:
|
that Angular knows to include it:
|
||||||
|
|
||||||
code-tabs
|
code-tabs
|
||||||
code-pane(language="javascript" name="TypeScript" format="linenums").
|
code-pane(language="javascript" name="TypeScript" format="linenums").
|
||||||
//Typescript
|
//Typescript
|
||||||
import {Component, View, bootstrap, For} from 'angular2/angular2';
|
import {Component, View, bootstrap, NgFor} from 'angular2/angular2';
|
||||||
@View({
|
@View({
|
||||||
...
|
...
|
||||||
directives: [For]
|
directives: [NgFor]
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
@ -173,7 +173,7 @@
|
||||||
...
|
...
|
||||||
new angular.ViewAnnotation({
|
new angular.ViewAnnotation({
|
||||||
...
|
...
|
||||||
directives: [angular.For]
|
directives: [angular.NgFor]
|
||||||
})
|
})
|
||||||
];
|
];
|
||||||
|
|
||||||
|
@ -185,13 +185,13 @@
|
||||||
p Let's look at the few lines that do the work again:
|
p Let's look at the few lines that do the work again:
|
||||||
code-example(language="html" format="linenums").
|
code-example(language="html" format="linenums").
|
||||||
//HTML
|
//HTML
|
||||||
<li *for="#name of names">
|
<li *ng-for="#name of names">
|
||||||
{{ name }}
|
{{ name }}
|
||||||
</li>
|
</li>
|
||||||
p The way to read this is:
|
p The way to read this is:
|
||||||
ul
|
ul
|
||||||
li.
|
li.
|
||||||
<code>*for</code> : create a DOM element for each item in an
|
<code>*ng-for</code> : create a DOM element for each item in an
|
||||||
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols">iterable</a>
|
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols">iterable</a>
|
||||||
like an array
|
like an array
|
||||||
li <code>#name</code> : refer to individual values of the iterable as 'name'
|
li <code>#name</code> : refer to individual values of the iterable as 'name'
|
||||||
|
@ -236,7 +236,7 @@
|
||||||
code-pane(language="javascript" name="TypeScript" format="linenums").
|
code-pane(language="javascript" name="TypeScript" format="linenums").
|
||||||
@Component({
|
@Component({
|
||||||
...
|
...
|
||||||
<span class='otl'>injectables: [FriendsService]</span>
|
<span class='otl'>appInjector: [FriendsService]</span>
|
||||||
})
|
})
|
||||||
class DisplayComponent {
|
class DisplayComponent {
|
||||||
myName: string;
|
myName: string;
|
||||||
|
@ -257,11 +257,11 @@
|
||||||
DisplayComponent.annotations = [
|
DisplayComponent.annotations = [
|
||||||
new angular.ComponentAnnotation({
|
new angular.ComponentAnnotation({
|
||||||
selector: "display",
|
selector: "display",
|
||||||
<span class='otl'>injectables: [FriendsService]</span>
|
<span class='otl'>appInjector: [FriendsService]</span>
|
||||||
}),
|
}),
|
||||||
new angular.ViewAnnotation({
|
new angular.ViewAnnotation({
|
||||||
template: '{{ myName }} <ul> <li *for="#name of names">{{ name }}</li> </ul>',
|
template: '{{ myName }} <ul> <li *for="#name of names">{{ name }}</li> </ul>',
|
||||||
directives: [angular.For]
|
directives: [angular.NgFor]
|
||||||
})
|
})
|
||||||
];
|
];
|
||||||
<span class='otl'>DisplayComponent.parameters = [[FriendsService]];</span>
|
<span class='otl'>DisplayComponent.parameters = [[FriendsService]];</span>
|
||||||
|
@ -269,25 +269,25 @@
|
||||||
angular.bootstrap(DisplayComponent);
|
angular.bootstrap(DisplayComponent);
|
||||||
});
|
});
|
||||||
.l-main-section
|
.l-main-section
|
||||||
h2#Conditionally-displaying-data-with-If Conditionally displaying data with If
|
h2#Conditionally-displaying-data-with-NgIf Conditionally displaying data with NgIf
|
||||||
p.
|
p.
|
||||||
Lastly, before we move on, let's handle showing parts of our UI conditionally with <code>If</code>. The
|
Lastly, before we move on, let's handle showing parts of our UI conditionally with <code>NgIf</code>. The
|
||||||
<code>If</code> directive adds or removes elements from the DOM based on the expression you provide.
|
<code>NgIf</code> directive adds or removes elements from the DOM based on the expression you provide.
|
||||||
p See it in action by adding a paragraph at the end of your template
|
p See it in action by adding a paragraph at the end of your template
|
||||||
pre.prettyprint.lang-html
|
pre.prettyprint.lang-html
|
||||||
code.
|
code.
|
||||||
<p *if="names.length > 3">You have many friends!</p>
|
<p *ng-if="names.length > 3">You have many friends!</p>
|
||||||
p You'll also need to add the <code>If</code> directive so Angular knows to include it.
|
p You'll also need to add the <code>NgIf</code> directive so Angular knows to include it.
|
||||||
|
|
||||||
code-tabs
|
code-tabs
|
||||||
code-pane(language="javascript" name="TypeScript" format="linenums").
|
code-pane(language="javascript" name="TypeScript" format="linenums").
|
||||||
//Typescript
|
//Typescript
|
||||||
import {Component, View, bootstrap, For, If} from 'angular2/angular2';
|
import {Component, View, bootstrap, NgFor, NgIf} from 'angular2/angular2';
|
||||||
...
|
...
|
||||||
directives: [For, If]
|
directives: [NgFor, NgIf]
|
||||||
code-pane(language="javascript" name="ES5" format="linenums").
|
code-pane(language="javascript" name="ES5" format="linenums").
|
||||||
//ES5
|
//ES5
|
||||||
directives: [angular.For, angular.If]
|
directives: [angular.NgFor, angular.NgIf]
|
||||||
p.
|
p.
|
||||||
As there are currently 6 items in the list, you'll see the message congratulating you on your many friends.
|
As there are currently 6 items in the list, you'll see the message congratulating you on your many friends.
|
||||||
Remove three items from the list, reload your browser, and see that the message no longer displays.
|
Remove three items from the list, reload your browser, and see that the message no longer displays.
|
||||||
|
@ -295,20 +295,20 @@
|
||||||
code-tabs
|
code-tabs
|
||||||
code-pane(language="javascript" name="TypeScript" format="linenums").
|
code-pane(language="javascript" name="TypeScript" format="linenums").
|
||||||
//TypeScript
|
//TypeScript
|
||||||
import {Component, View, bootstrap, For, If} from 'angular2/angular2';
|
import {Component, View, bootstrap, NgFor, NgIf} from 'angular2/angular2';
|
||||||
...
|
...
|
||||||
@View({
|
@View({
|
||||||
<span class='otl'>template</span>: `
|
<span class='otl'>template</span>: `
|
||||||
<p>My name: {{ myName }}</p>
|
<p>My name: {{ myName }}</p>
|
||||||
<p>Friends:</p>
|
<p>Friends:</p>
|
||||||
<ul>
|
<ul>
|
||||||
<li *for="#name of names">
|
<li *ng-for="#name of names">
|
||||||
{{ name }}
|
{{ name }}
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
<p *if="names.length > 3">You have many friends!</p>
|
<p *ng-if="names.length > 3">You have many friends!</p>
|
||||||
`,
|
`,
|
||||||
directives: [For, If]
|
directives: [NgFor, NgIf]
|
||||||
})
|
})
|
||||||
class DisplayComponent {
|
class DisplayComponent {
|
||||||
...
|
...
|
||||||
|
@ -333,12 +333,12 @@
|
||||||
'<p>My name: {{ myName }}</p>' +
|
'<p>My name: {{ myName }}</p>' +
|
||||||
'<p>Friends:</p>' +
|
'<p>Friends:</p>' +
|
||||||
'<ul>' +
|
'<ul>' +
|
||||||
'<li *for="#name of names">' +
|
'<li *ng-for="#name of names">' +
|
||||||
'{{ name }}' +
|
'{{ name }}' +
|
||||||
'</li>' +
|
'</li>' +
|
||||||
'</ul>' +
|
'</ul>' +
|
||||||
'<p *if="names.length > 3">You have many friends!</p>'',
|
'<p *ng-if="names.length > 3">You have many friends!</p>'',
|
||||||
directives: [angular.For, angular.If]
|
directives: [angular.NgFor, angular.NgIf]
|
||||||
})
|
})
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue