You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: index.html
+35-20Lines changed: 35 additions & 20 deletions
Original file line number
Diff line number
Diff line change
@@ -947,12 +947,12 @@ <h4 class="exercise-start">
947
947
<p><strong>TIP</strong>: If you haven’t played with Angular 2 routing before, you can refer to <ahref="https://angular.io/docs/ts/latest/tutorial/toh-pt5.html">Angular’s tutorial on the topic</a> for some background.</p>
948
948
</blockquote>
949
949
<p>The other new concept in this example is <code><page-router-outlet></code>, which is your app’s first directive. You can check out Angular’s docs for <ahref="https://angular.io/docs/ts/latest/api/core/Directive-decorator.html">details on what directives do</a>, but the simplest way to think of them is as something that can affect the markup you put in your <code>template</code>—in this case <code><page-router-outlet></code>.</p>
950
-
<p>Angular 2 provides a <code><router-outlet></code> directive for web apps, and NativeScript extends that directive with its own <code><page-router-outlet></code> directive that handles the unique environment of iOS and Android apps. To see how it works lets add another page.</p>
950
+
<p>Angular 2 provides a <code><router-outlet></code> directive for web apps, and NativeScript extends that directive with its own <code><page-router-outlet></code> directive that handles the unique environment of iOS and Android apps. To see how it works let’s add another page.</p>
951
951
<h4class="exercise-start">
952
952
<b>Exercise</b>: Create the list page
953
953
</h4>
954
954
955
-
<p>Open pages/list/list.component.ts and paste in this:</p>
955
+
<p>Open <code>pages/list/list.component.ts</code> and paste in the following code, which you’ll use as the start of a simple list page:</p>
956
956
<pre><codeclass="lang-TypeScript">import {Component} from "angular2/core";
957
957
958
958
@Component({
@@ -962,16 +962,20 @@ <h4 class="exercise-start">
962
962
})
963
963
export class ListPage {}
964
964
</code></pre>
965
-
<p>Next, open pages/list/list.html and paste in this:</p>
965
+
<p>For now, we’ll keep the list page simple so you can see how the routing works. But so that there’s something to see, open <code>pages/list/list.html</code> and paste the following label:</p>
<p>Now go to app/shared/user/user.service.ts and add this function:</p>
977
+
<p>Angular 2 now knows about the list page, but we still need to navigate the user to that page at the appropriate time. Our next step is to allow users to log into their accounts, and to take navigate them to the new list page after they successfully authenticate.</p>
978
+
<p>To do that, start by opening <code>app/shared/user/user.service.ts</code> and the <code>login()</code> function below to the existing <code>UserService</code> class:</p>
(error) => alert("Unfortunately we could not find your account.")
1000
-
);
1001
-
</code></pre>
1002
-
<p>To make this work, in the same file, add this to the top:</p>
999
+
<p>This code hits one of our existing backend endpoints, and stores off a authentication token that we’ll use later in this guide.</p>
1000
+
<p>To use this <code>login()</code> function, return to <code>app/login/login.component.ts</code>, and add the following import to the top of the file:</p>
1003
1001
<pre><codeclass="lang-TypeScript">import {Router} from "angular2/router";
1004
1002
</code></pre>
1005
-
<p>And this to the constructor:</p>
1003
+
<p>Next, replace the current <code>constructor()</code> declaration with the code below, which adds Angular 2’s <code>Router</code> service:</p>
(error) => alert("Unfortunately we could not find your account.")
1012
+
);
1013
+
}
1014
+
</code></pre>
1015
+
<blockquote>
1016
+
<p><strong>NOTE</strong>:</p>
1017
+
<ul>
1018
+
<li>You don’t have to add <code>Router</code> to your <code>LoginPage</code> component’s <code>providers</code> array because it’s already included in the parent <code>AppComponent</code> component’s <code>providers</code> list.</li>
1019
+
<li>Refer to Angular’s documentation for a <ahref="https://angular.io/docs/ts/latest/api/router/Router-class.html">full list of the API available on the <code>Router</code> service</a>.</li>
1020
+
</ul>
1021
+
</blockquote>
1008
1022
<divclass="exercise-end"></div>
1009
1023
1010
-
<p>You can login now! And navigate! </p>
1011
-
<p><imgalt="Login with basic information" src="images/chapter3/login_router.gif"></p>
1012
-
<p>Talk about how in NativeScript you get native behavior automatically—aka a back button on iOS and a hardware back button on Android.</p>
1013
-
<p>Then transition to modules.</p>
1024
+
<p>After this change you can now navigate between the login and list pages in your app:</p>
1025
+
<p><imgsrc="images/chapter3/android/7.gif" alt="Navigating on Android">
1026
+
<imgsrc="images/chapter3/ios/7.gif" alt="Navigating on iOS"></p>
1027
+
<p>The power of NativeScript is you have the ability to use the same Angular conventions that you’d use in a web app—<code>@RouteConfig</code>, <code>Router</code>, and so forth—yet get an app that fits right in on iOS and Android. Notice how on Android the hardware back button works as expected, and how your iOS app uses built-in iOS animations and conventions such as the back button.</p>
1028
+
<p>And we’re just getting started. NativeScript provides a number of other ways to tie into native device functionality out of the box through NativeScript modules. Let’s look at how they work.</p>
Copy file name to clipboardExpand all lines: src/chapters/chapter3.md
+35-21Lines changed: 35 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -531,13 +531,13 @@ The main new concept here is the `@RouteConfig` decorator, which you use to prov
531
531
532
532
The other new concept in this example is `<page-router-outlet>`, which is your app’s first directive. You can check out Angular’s docs for [details on what directives do](https://angular.io/docs/ts/latest/api/core/Directive-decorator.html), but the simplest way to think of them is as something that can affect the markup you put in your `template`—in this case `<page-router-outlet>`.
533
533
534
-
Angular 2 provides a `<router-outlet>` directive for web apps, and NativeScript extends that directive with its own `<page-router-outlet>` directive that handles the unique environment of iOS and Android apps. To see how it works lets add another page.
534
+
Angular 2 provides a `<router-outlet>` directive for web apps, and NativeScript extends that directive with its own `<page-router-outlet>` directive that handles the unique environment of iOS and Android apps. To see how it works let’s add another page.
535
535
536
536
<h4class="exercise-start">
537
537
<b>Exercise</b>: Create the list page
538
538
</h4>
539
539
540
-
Open pages/list/list.component.ts and paste in this:
540
+
Open `pages/list/list.component.ts` and paste in the following code, which you’ll use as the start of a simple list page:
541
541
542
542
```TypeScript
543
543
import {Component} from"angular2/core";
@@ -550,25 +550,30 @@ import {Component} from "angular2/core";
550
550
exportclassListPage {}
551
551
```
552
552
553
-
Next, open pages/list/list.html and paste in this:
553
+
For now, we’ll keep the list page simple so you can see how the routing works. But so that there’s something to see, open `pages/list/list.html` and paste the following label:
554
554
555
555
```XML
556
556
<Labeltext="Hello world"></Label>
557
557
```
558
558
559
-
Now, go back to app/app.component.ts and paste in this at the top of the file:
559
+
After that, go back to `app/app.component.ts` and paste the following import in at the top of the file:
Now go to app/shared/user/user.service.ts and add this function:
574
+
Angular 2 now knows about the list page, but we still need to navigate the user to that page at the appropriate time. Our next step is to allow users to log into their accounts, and to take navigate them to the new list page after they successfully authenticate.
575
+
576
+
To do that, start by opening `app/shared/user/user.service.ts` and the `login()` function below to the existing `UserService` class:
572
577
573
578
```TypeScript
574
579
login(user: User) {
@@ -592,34 +597,43 @@ login(user: User) {
592
597
}
593
598
```
594
599
595
-
Finally, go to `app/login/login.component.ts` and change the login function to do this:
600
+
This code hits one of our existing backend endpoints, and stores off a authentication token that we’ll use later in this guide.
601
+
602
+
To use this `login()` function, return to `app/login/login.component.ts`, and add the following import to the top of the file:
596
603
597
604
```TypeScript
598
-
this._userService.login(this.user)
599
-
.subscribe(
600
-
() =>this._router.navigate(["List"]),
601
-
(error) =>alert("Unfortunately we could not find your account.")
602
-
);
605
+
import {Router} from"angular2/router";
603
606
```
604
607
605
-
To make this work, in the same file, add this to the top:
608
+
Next, replace the current `constructor()` declaration with the code below, which adds Angular 2’s `Router` service:
(error) =>alert("Unfortunately we could not find your account.")
622
+
);
623
+
}
615
624
```
616
625
626
+
> **NOTE**:
627
+
> * You don’t have to add `Router` to your `LoginPage` component’s `providers` array because it’s already included in the parent `AppComponent` component’s `providers` list.
628
+
> * Refer to Angular’s documentation for a [full list of the API available on the `Router` service](https://angular.io/docs/ts/latest/api/router/Router-class.html).
629
+
617
630
<div class="exercise-end"></div>
618
631
619
-
You can login now! And navigate!
632
+
After this change you can now navigate between the login and list pages in your app:
620
633
621
-
<img alt="Login with basic information" src="images/chapter3/login_router.gif">
634
+

635
+

622
636
623
-
Talk about how in NativeScript you get native behavior automatically—aka a back button on iOS and a hardware back button on Android.
637
+
The power of NativeScript is you have the ability to use the same Angular conventions that you’d use in a web app—`@RouteConfig`, `Router`, and so forth—yet get an app that fits right in on iOS and Android. Notice how on Android the hardware back button works as expected, and how your iOS app uses built-in iOS animations and conventions such as the back button.
624
638
625
-
Then transition to modules.
639
+
And we’re just getting started. NativeScript provides a number of other ways to tie into native device functionality out of the box through NativeScript modules. Let’s look at how they work.
0 commit comments