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
<p>Open <code>app/app.component.ts</code>, find the existing sign in button within your component’s <code>template</code> (<code><Button text="Sign in"></Button></code>), and replace it with the following code:</p>
<p>Next, in the same file, replace the current <code>AppComponent</code> declaration with the one shown below:</p>
548
548
<pre><codeclass="lang-JavaScript">export class AppComponent {
549
-
signIn() {
549
+
submit() {
550
550
console.log("hello");
551
551
}
552
552
}
553
553
</code></pre>
554
554
<divclass="exercise-end"></div>
555
555
556
-
<p>The <code>(eventName)="functionName()"</code> syntax is part of <ahref="https://angular.io/docs/ts/latest/guide/template-syntax.html#!#event-binding">Angular 2’s event binding system</a>, which lets you bind an event that occurs on a UI element to a function in your component’s class. In this case, the <code>(tap)="signIn()"</code> syntax tells Angular to run the <code>AppComponent</code> class’s <code>signIn()</code> function whenever the user taps the sign in button.</p>
556
+
<p>The <code>(eventName)="functionName()"</code> syntax is part of <ahref="https://angular.io/docs/ts/latest/guide/template-syntax.html#!#event-binding">Angular 2’s event binding system</a>, which lets you bind an event that occurs on a UI element to a function in your component’s class. In this case, the <code>(tap)="submit()"</code> syntax tells Angular to run the <code>AppComponent</code> class’s <code>submit()</code> function whenever the user taps the sign in button.</p>
557
557
<p>To verify this binding works tap the “Sign In” button in your app; you should see “hello” logged in your terminal or command prompt as such:</p>
558
558
<p><imgalt="Terminal showing the word hello logged" src="images/chapter3/terminal-1.png" class="plain"></p>
559
559
<blockquote>
@@ -571,10 +571,10 @@ <h4 class="exercise-start">
571
571
<b>Exercise</b>: Using Angular 2 attribute binding
572
572
</h4>
573
573
574
-
<p>In <code>app/app.component.ts</code> replace the current <code>AppComponent</code> declaration with the one shown below, which adds a new <code>email</code> property, and changes the <code>signIn()</code> method to display its value:</p>
574
+
<p>In <code>app/app.component.ts</code> replace the current <code>AppComponent</code> declaration with the one shown below, which adds a new <code>email</code> property, and changes the <code>submit()</code> method to display its value:</p>
575
575
<pre><codeclass="lang-TypeScript">export class AppComponent {
<p><strong>NOTE</strong>: It’s very easy to confuse Angular 2’s event binding syntax <code>(eventName)="functionName()"</code> with its attribute binding syntax <code>[attributeName]="propertyName"</code> 🤔. Don’t worry though; if you get them backwards, the problem is usually easy to debug, as the functionality you’re attempting to add just won’t work. These syntaxes are common enough that you’ll be using them a lot, and eventually you should be able to commit them to memory.</p>
593
593
</blockquote>
594
594
<p>This attribute binding approach works really well when you need the data binding to be one way—that is, when you need TypeScript properties, and changes to those properties done in TypeScript code, to appear in the user interface. But in the case of user interface controls that accept user input, such as the text field in this example, usually you need data binding to work two way—that is, you additionally need changes the user makes to be reflected in your TypeScript code.</p>
595
-
<p>To show that the current example’s data binding is only one way, head back to your app, change the email text field’s value (type a few extra letters or something like that), and then tap the “Sign In” button. Because your <code>signIn()</code> function alerts the current value of your component’s <code>email</code> property—<code>alert("You’re using: " + this.email)</code>—you might expect to see the updated value in the alert. Instead, however, you see the original value. Notice how the typed text and the alert value don’t match in the screenshot below.</p>
595
+
<p>To show that the current example’s data binding is only one way, head back to your app, change the email text field’s value (type a few extra letters or something like that), and then tap the “Sign In” button. Because your <code>submit()</code> function alerts the current value of your component’s <code>email</code> property—<code>alert("You’re using: " + this.email)</code>—you might expect to see the updated value in the alert. Instead, however, you see the original value. Notice how the typed text and the alert value don’t match in the screenshot below.</p>
596
596
<p><imgsrc="images/chapter3/android/2.png" alt="Android with email address that do not match"></p>
597
597
<p>To fix this, you need to switch to Angular 2’s two-way data binding syntax.</p>
598
598
<h4class="exercise-start">
@@ -609,6 +609,31 @@ <h4 class="exercise-start">
609
609
<p>Don’t worry too much about the details here while we’re still getting started. In your head you can think of <code>[(ngModel)]</code> as the way to implement two-way data binding when you need it on form controls. And to show that it works, if you again modify your app’s email address and click the “Sign In” button, you’ll see the updated value in the alert as expected:</p>
610
610
<p><imgsrc="images/chapter3/android/3.png" alt="Android with email addresses that do match"></p>
611
611
<p>At this point, you have a basic login screen setup with two-way data binding—not bad for 20 some lines of code of TypeScript. (Think about how much code you’d have to write in Android Studio <em>and</em> Xcode to accomplish the same task.) To this point though you’ve been placing all of your logic in a single TypeScript file, which doesn’t scale all that well for real-world applications.</p>
612
+
<p>TODO: Transition</p>
613
+
<h4class="exercise-start">
614
+
<b>Exercise</b>: ???
615
+
</h4>
616
+
617
+
<p>app.component.ts — Replace the two buttons with this</p>
<p>Before we tie this app to a backend and make this login screen fully functional, let’s take a step back and setup a structure that can scale.</p>
613
638
<h3id="structuring-your-app">Structuring your app</h3>
614
639
<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>. However, keeping NativeScript apps modular 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>
@@ -632,12 +657,17 @@ <h4 class="exercise-start">
632
657
<p>Next, replace the existing <code>AppComponent</code> definition with the one below, which uses the <code>User</code> class you just imported.</p>
633
658
<pre><codeclass="lang-JavaScript">export class AppComponent {
<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>
<p>TODO: Better explanation. Move the full template into the login.html file and use <code>templateUrl</code>. </p>
649
680
<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>
650
681
<pre><codeclass="lang-JavaScript">import {Component} from "angular2/core";
651
682
import {User} from "./shared/user/user";
0 commit comments