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
</code></pre><h3id="start-your-app">Start your app</h3>
94
94
<p>With the NativeScript CLI installed, it's time to start building your app. Normally, you would <ahref="https://github.com/NativeScript/NativeScript-cli#create-project">use the <code>tns create</code> command to create an empty NativeScript application</a>. For this guide however, we've scaffolded out a boilerplate project to act as a starting point for <ahref="https://github.com/NativeScript/sample-Groceries">Groceries</a>.</p>
95
95
<blockquote>
96
-
<p><strong>NOTE</strong>: After completing this guide, if you’d like to start a new NativeScript and Angular 2 app—one that doesn’t clone Groceries—you can use the NativeScript CLI’s <code>tns create</code> command as such:
<p><strong>NOTE</strong>: After completing this guide, if you’d like to start a new NativeScript and Angular 2 app—one that doesn’t clone Groceries—you can use the NativeScript CLI’s <code>tns create</code> command as such:</p>
<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>definition with the one shown below:</p>
487
+
<p>Next, in the same file, replace the current <code>AppComponent</code>declaration with the one shown below:</p>
488
488
<pre><codeclass="lang-JavaScript">export class AppComponent {
489
489
signIn() {
490
490
console.log("hello");
@@ -493,10 +493,47 @@ <h4 class="exercise-start">
493
493
</code></pre>
494
494
<divclass="exercise-end"></div>
495
495
496
-
<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 you just used tells Angular to run the <code>AppComponent</code> class’s <code>signIn()</code> function whenever the user taps the sign in button.</p>
497
-
<p>To verify this binding works you can try tapping the sign in button now; you should see “hello” logged in your terminal or command prompt as is shown below:</p>
496
+
<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>
497
+
<p>To verify this binding works try tapping the sign in button in your app; you should see “hello” logged in your terminal or command prompt as such:</p>
498
498
<p><imgsrc="images/chapter3/terminal-1.png" alt="Terminal showing the word hello logged"></p>
499
+
<blockquote>
500
+
<p><strong>TIP</strong>:</p>
501
+
<ul>
502
+
<li>In NativeScript you can find a list of events available in the appropriate UI element’s API documentation. For instance, the <ahref="http://docs.nativescript.org/ApiReference/ui/button/Button">button element’s API documentation</a> lists its <code>tap</code> event.</li>
503
+
<li>The Angular 2 docs have a helpful <ahref="https://angular.io/docs/ts/latest/guide/cheatsheet.html">cheat sheet</a> that includes the various syntaxes available when building templates. Don’t worry too much about knowing how all these work at the moment; we’ll progressively introduce the most common syntaxes in this guide.</li>
504
+
</ul>
505
+
</blockquote>
506
+
<p>With the <code>tap</code> event in place, we now have a way of tying our UI elements in our template to TypeScript code. To make a login page actually work though, we need to introduce one other way of connecting a template to code: data binding.</p>
499
507
<h3id="data-binding">Data Binding</h3>
508
+
<p>Angular 2 provides several ways to bind data in your TypeScript code to UI controls, and through the magic of NativeScript those same methods are available in your iOS and Android apps.</p>
509
+
<p>The first of these is a way to bind UI attributes to properties defined in your TypeScript class. Let’s look at how it works.</p>
510
+
<h4class="exercise-start">
511
+
<b>Exercise</b>: Using Angular 2 attribute binding
512
+
</h4>
513
+
514
+
<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>
515
+
<pre><codeclass="lang-JavaScript">export class AppComponent {
<p>Next, find the first <code><TextField></code> in your component’s <code>template</code> and replace it with the code below, which adds a new <code>text</code> attribute:</p>
<p>The key thing to note here is the <code>[text]="email"</code> attribute on the <code><TextField></code>. This is Angular 2’s syntax for attribute binding, and it allows you to bind the value of an attribute to a property in your TypeScript class. In this case, the <code>text</code> attribute of the <code><TextField></code>—which is roughly equivalent to a DOM <code><input></code>’s <code>value</code> attribute—is bound to the <code>AppComponent</code>’s <code>email</code> attribute. Therefore the app now has an email address prefilled when it loads:</p>
529
+
<p><imgsrc="images/chapter3/android/1.png" alt="Android with prefilled email">
530
+
<imgsrc="images/chapter3/ios/1.png" alt="iOS with prefilled email"></p>
531
+
<blockquote>
532
+
<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 really 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>
533
+
</blockquote>
534
+
<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—and actually, it’s far easier to show this limitation in the current code.</p>
535
+
<p>Head back to your app, change the text field 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>
536
+
<p><imgsrc="images/chapter3/ios/2.png" alt="iOS with email address that doesn’t match"></p>
Copy file name to clipboardExpand all lines: src/chapters/chapter1.md
+3-1Lines changed: 3 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -31,7 +31,9 @@ $ tns
31
31
With the NativeScript CLI installed, it's time to start building your app. Normally, you would [use the `tns create` command to create an empty NativeScript application](https://github.com/NativeScript/NativeScript-cli#create-project). For this guide however, we've scaffolded out a boilerplate project to act as a starting point for [Groceries](https://github.com/NativeScript/sample-Groceries).
32
32
33
33
> **NOTE**: After completing this guide, if you’d like to start a new NativeScript and Angular 2 app—one that doesn’t clone Groceries—you can use the NativeScript CLI’s `tns create` command as such:
0 commit comments