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
+23-7Lines changed: 23 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -653,7 +653,7 @@ <h4 class="exercise-start">
653
653
<h3id="structuring-your-app">Structuring your app</h3>
654
654
<p>There are many reasons to segment any application into modular units, and you can <ahref="https://en.wikipedia.org/wiki/Modular_programming">read about the various benefits on Wikipedia</a>. Modularizing NativeScript apps, in addition, has one unique benefit: the ability to share the code you write between Angular-2-built web apps, and Angular-2-built native apps.</p>
655
655
<p>Even if you have no plans to create an Angular 2 web app, separating out your code is still advantageous for a number of other reasons—testability, ease of maintenance, and so forth—but if you <em>do</em> have plans to build an Angular 2 web app, having a chunk of functionality that you can reuse in your native and web apps can be an invaluable time saver.</p>
656
-
<p>To see how this works in action, let’s edit some files in the <code>/shared</code> folder and set them up to be imported.</p>
656
+
<p>To see how this works in action, let’s edit some files in the <code>shared</code> folder and set them up to be imported.</p>
657
657
<h4class="exercise-start">
658
658
<b>Exercise</b>: Add a model object
659
659
</h4>
@@ -665,12 +665,11 @@ <h4 class="exercise-start">
665
665
}
666
666
</code></pre>
667
667
<p>This code defines a simple <ahref="http://www.typescriptlang.org/Handbook#classes">TypeScript class</a> that does nothing more than define two properties—<code>email</code> and <code>password</code>. Note the use of <ahref="http://www.typescriptlang.org/Handbook#modules-going-external">TypeScript’s <code>export</code> keyword</a>, as we’ll see why that’s important momentarily.</p>
668
-
<p>Next, open <code>app/app.component.ts</code>, and first add the following <code>import</code> to the top of the file:</p>
668
+
<p>Next, open <code>app/app.component.ts</code>, and add the following <code>import</code> to the top of the file:</p>
669
669
<pre><codeclass="lang-JavaScript">import {User} from "./shared/user/user";
670
670
</code></pre>
671
671
<p>Here you import the <code>User</code> class that you just defined. Note the parallel between the <code>export</code> command used in the previous example and the <code>import</code> command used here. The reason the <code>User</code> class is available to import is because it was explicitly exported. You’ll see other examples of <code>import</code> and <code>export</code> as you go through this guide.</p>
672
672
<p>Next, replace the existing <code>AppComponent</code> definition with the one below, which uses the <code>User</code> class you just imported.</p>
673
-
<!-- Todo, explain the isLoggingIn piece - from the exercise above I think. The button XML needs to be edited -->
674
673
<pre><codeclass="lang-JavaScript">export class AppComponent {
675
674
user: User;
676
675
isLoggingIn = true;
@@ -687,14 +686,28 @@ <h4 class="exercise-start">
687
686
}
688
687
</code></pre>
689
688
<p>Instead of storing data on the <code>AppComponent</code> directly, you’re now using the <code>User</code> model object, which is reusable outside of this page and even outside of this application. You instantiate an instance of the <code>User</code> class in a new <code>constructor</code> function, which Angular 2 invokes when it bootstraps your application.</p>
690
-
<p>Your final step is to use this new model object in your template. To do that, replace the two existing <code><TextField></code>s with the code shown below, which updates the <code>[(ngModel)]</code> bindings to point at the new <code>User</code> object:</p>
689
+
<p>Your next step is to use this new model object in your template. To do that, replace the two existing <code><TextField></code>s with the code shown below, which updates the <code>[(ngModel)]</code> bindings to point at the new <code>User</code> object:</p>
<p>TODO: Better explanation. Move the full template into the login.html file and use <code>templateUrl</code>. </p>
696
-
<p>If you got lost during this section, here’s a copy-and-paste friendly version of the full <code>app.component.ts</code> you should have at this point:</p>
697
-
<pre><codeclass="lang-JavaScript">import {Component} from "angular2/core";
694
+
<p>As one final change, because this template is getting to be a bit complex, let’s move it into a separate file. Open <code>app/pages/login/login.html</code> and paste in the following code:</p>
<Button [text]="isLoggingIn ? 'Sign up' : 'Back to login'" (tap)="toggleDisplay()"></Button>
704
+
</StackLayout>
705
+
</code></pre>
706
+
<p>Finally, in <code>app/app.component.ts</code>, replace the existing <code>template</code> property with the new <code>templateUrl</code> property shown below:</p>
<p>In case you got lost during this section, here’s a copy-and-paste friendly of the <code>app/app.component.ts</code> file you should have at this point:</p>
710
+
<pre><codeclass="lang-TypeScript">import {Component} from "angular2/core";
698
711
import {User} from "./shared/user/user";
699
712
700
713
@Component({
@@ -719,6 +732,9 @@ <h4 class="exercise-start">
719
732
</code></pre>
720
733
<divclass="exercise-end"></div>
721
734
735
+
<blockquote>
736
+
<p><strong>TIP</strong>: With Angular 2 components you have the ability to specify templates and CSS styling in two places—directly within the component, or in external files. For simple components feel free to choose either approach based on your personal preference, but once your templates/styles get to ~10 lines of code, consider using external files exclusively, as mixing non-trivial XML, CSS, and JavaScript code makes your component code less readable.</p>
737
+
</blockquote>
722
738
<p>With this setup you now have a <code>User</code> class that you can share across pages in your app and even across applications. But a model object that’s four simple lines of code isn’t all that exciting. Where this approach really pays off is when you’re able to share your business logic, and the code that hits your backend systems. In Angular 2 those classes are known as services. Let’s look at them next.</p>
723
739
<h3id="services">Services</h3>
724
740
<p>A login screen isn’t all that useful if it doesn’t actually log users into anything. Therefore, our next task is to take the user’s email address and password, and send them to a backend endpoint to retrieve an authentication token we’ll use later in this guide. We’ll build this functionality as an Angular 2 service.</p>
Copy file name to clipboardExpand all lines: src/chapters/chapter3.md
+27-8Lines changed: 27 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -151,7 +151,7 @@ There are many reasons to segment any application into modular units, and you ca
151
151
152
152
Even if you have no plans to create an Angular 2 web app, separating out your code is still advantageous for a number of other reasons—testability, ease of maintenance, and so forth—but if you _do_ have plans to build an Angular 2 web app, having a chunk of functionality that you can reuse in your native and web apps can be an invaluable time saver.
153
153
154
-
To see how this works in action, let’s edit some files in the `/shared` folder and set them up to be imported.
154
+
To see how this works in action, let’s edit some files in the `shared` folder and set them up to be imported.
155
155
156
156
<h4class="exercise-start">
157
157
<b>Exercise</b>: Add a model object
@@ -168,7 +168,7 @@ export class User {
168
168
169
169
This code defines a simple [TypeScript class](http://www.typescriptlang.org/Handbook#classes) that does nothing more than define two properties—`email` and `password`. Note the use of [TypeScript’s `export` keyword](http://www.typescriptlang.org/Handbook#modules-going-external), as we’ll see why that’s important momentarily.
170
170
171
-
Next, open `app/app.component.ts`, and first add the following `import` to the top of the file:
171
+
Next, open `app/app.component.ts`, and add the following `import` to the top of the file:
172
172
173
173
```JavaScript
174
174
import {User} from"./shared/user/user";
@@ -178,7 +178,6 @@ Here you import the `User` class that you just defined. Note the parallel betwee
178
178
179
179
Next, replace the existing `AppComponent` definition with the one below, which uses the `User` class you just imported.
180
180
181
-
<!-- Todo, explain the isLoggingIn piece - from the exercise above I think. The button XML needs to be edited -->
182
181
```JavaScript
183
182
exportclassAppComponent {
184
183
user: User;
@@ -198,19 +197,38 @@ export class AppComponent {
198
197
199
198
Instead of storing data on the `AppComponent` directly, you’re now using the `User` model object, which is reusable outside of this page and even outside of this application. You instantiate an instance of the `User` class in a new `constructor` function, which Angular 2 invokes when it bootstraps your application.
200
199
201
-
Your final step is to use this new model object in your template. To do that, replace the two existing `<TextField>`s with the code shown below, which updates the `[(ngModel)]` bindings to point at the new `User` object:
200
+
Your next step is to use this new model object in your template. To do that, replace the two existing `<TextField>`s with the code shown below, which updates the `[(ngModel)]` bindings to point at the new `User` object:
TODO: Better explanation. Move the full template into the login.html file and use `templateUrl`.
208
+
As one final change, because this template is getting to be a bit complex, let’s move it into a separate file. Open `app/pages/login/login.html`and paste in the following code:
210
209
211
-
If you got lost during this section, here’s a copy-and-paste friendly version of the full `app.component.ts` you should have at this point:
<Button [text]="isLoggingIn ? 'Sign up' : 'Back to login'" (tap)="toggleDisplay()"></Button>
220
+
</StackLayout>
221
+
```
222
+
223
+
Finally, in `app/app.component.ts`, replace the existing `template` property with the new `templateUrl` property shown below:
224
+
225
+
```TypeScript
226
+
templateUrl: "pages/login/login.html"
227
+
```
228
+
229
+
In case you got lost during this section, here’s a copy-and-paste friendly of the `app/app.component.ts` file you should have at this point:
230
+
231
+
```TypeScript
214
232
import {Component} from"angular2/core";
215
233
import {User} from"./shared/user/user";
216
234
@@ -233,11 +251,12 @@ export class AppComponent {
233
251
this.isLoggingIn=!this.isLoggingIn;
234
252
}
235
253
}
236
-
237
254
```
238
255
239
256
<divclass="exercise-end"></div>
240
257
258
+
> **TIP**: With Angular 2 components you have the ability to specify templates and CSS styling in two places—directly within the component, or in external files. For simple components feel free to choose either approach based on your personal preference, but once your templates/styles get to ~10 lines of code, consider using external files exclusively, as mixing non-trivial XML, CSS, and JavaScript code makes your component code less readable.
259
+
241
260
With this setup you now have a `User` class that you can share across pages in your app and even across applications. But a model object that’s four simple lines of code isn’t all that exciting. Where this approach really pays off is when you’re able to share your business logic, and the code that hits your backend systems. In Angular 2 those classes are known as services. Let’s look at them next.
0 commit comments