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-1Lines changed: 23 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -474,7 +474,29 @@ <h4 class="exercise-start">
474
474
</div>
475
475
<divclass="chapter">
476
476
<h2id="application-logic">Application Logic</h2>
477
-
<h3id="events-and-data-binding">Events and data binding</h3>
477
+
<p>In this chapter you’ll learn how to add JavaScript logic to your app, how to create services that talk to backend endpoints, and how to architect an app that uses multiple pages. There’s a lot to cover, so let’s start by discussing how to handle events and data binding.</p>
478
+
<h3id="events">Events</h3>
479
+
<p>Most user interfaces are driven by events. In NativeScript apps, those events are usually some user action, such as tapping, swiping, or rotating—and NativeScript abstracts the iOS- and Android-specific code for handling such events into a handful of easy-to-use APIs. Let’s start with the most common event you’ll use in a NativeScript app—<code>tap</code>.</p>
480
+
<h4class="exercise-start">
481
+
<b>Exercise</b>: Add a <code>tap</code> event handler
482
+
</h4>
483
+
484
+
<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>
488
+
<pre><codeclass="lang-JavaScript">export class AppComponent {
489
+
signIn() {
490
+
console.log("hello");
491
+
}
492
+
}
493
+
</code></pre>
494
+
<divclass="exercise-end"></div>
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>
498
+
<p><imgsrc="images/chapter3/terminal-1.png" alt="Terminal showing the word hello logged"></p>
Copy file name to clipboardExpand all lines: src/chapters/chapter3.md
+35-1Lines changed: 35 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,40 @@
1
1
## Application Logic
2
2
3
-
### Events and data binding
3
+
In this chapter you’ll learn how to add JavaScript logic to your app, how to create services that talk to backend endpoints, and how to architect an app that uses multiple pages. There’s a lot to cover, so let’s start by discussing how to handle events and data binding.
4
+
5
+
### Events
6
+
7
+
Most user interfaces are driven by events. In NativeScript apps, those events are usually some user action, such as tapping, swiping, or rotating—and NativeScript abstracts the iOS- and Android-specific code for handling such events into a handful of easy-to-use APIs. Let’s start with the most common event you’ll use in a NativeScript app—`tap`.
8
+
9
+
<h4class="exercise-start">
10
+
<b>Exercise</b>: Add a `tap` event handler
11
+
</h4>
12
+
13
+
Open `app/app.component.ts`, find the existing sign in button within your component’s `template` (`<Button text="Sign in"></Button>`), and replace it with the following code:
14
+
15
+
```JavaScript
16
+
<Button text="Sign in" (tap)="signIn()"></Button>
17
+
```
18
+
19
+
Next, in the same file, replace the current `AppComponent` definition with the one shown below:
20
+
21
+
```JavaScript
22
+
exportclassAppComponent {
23
+
signIn() {
24
+
console.log("hello");
25
+
}
26
+
}
27
+
```
28
+
29
+
<divclass="exercise-end"></div>
30
+
31
+
The `(eventName)="functionName()"` syntax is part of [Angular 2’s event binding system](https://angular.io/docs/ts/latest/guide/template-syntax.html#!#event-binding), which lets you bind an event that occurs on a UI element to a function in your component’s class. In this case, the `(tap)="signIn()"` syntax you just used tells Angular to run the `AppComponent` class’s `signIn()` function whenever the user taps the sign in button.
32
+
33
+
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:
34
+
35
+

0 commit comments