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>Notice the interesting way that the <code>Component</code> class is used—with the syntax <code>@Component</code>. This is a <ahref="https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/Decorators.md">TypeScript decorator</a>, which allows you to annotate a TypeScript class or method with additional information. For now, you can think of it as a way of adding some metadata configuration to the currently empty <code>AppComponent</code> class. Specifically, the <code>@Component</code> decorator’s <code>template</code> property tells NativeScript how to render this component on the screen. In fact, the <code><Label text="hello NativeScript"></Label></code> syntax is why you saw “hello NativeScript” when you ran this app earlier.</p>
309
309
<p>However, this syntax may look a bit odd if you come from a web development background. On the web, the <code><label></code> HTML element doesn’t have a <code>text</code> attribute, so why do we see it here? Let’s dive into this by looking at how NativeScript UI elements work.</p>
310
310
<blockquote>
311
-
<p><strong>NOTE</strong>: Curious about the <code>@Component</code> decorator’s <code>selector</code> property? The property defines how a component can be used within another component’s template. For instance a component that defines its <code>selector</code> with <code>selector: "foo-bar"</code> can be used by another component as <code>template: "<foo-bar></foo-bar>"</code>. NativeScript is smart enough to use your first Angular 2 component automatically; therefore, the <code>selector</code> property is currently irrelevant. We’ll use the <code>selector</code> property later in this guide though.</p>
311
+
<p><strong>NOTE</strong>: Curious about the <code>@Component</code> decorator’s <code>selector</code> property? The property defines how a component can be used within another component’s template. For instance a component that defines its <code>selector</code> with <code>selector: "foo-bar"</code> can be used by another component as <code>template: "<foo-bar></foo-bar>"</code>. NativeScript is smart enough to use your first Angular 2 component automatically; therefore, the <code>selector</code> property of this first component is irrelevant. We’ll use the <code>selector</code> property later in this guide though.</p>
<p>The primary difference between building an Angular 2 app for the web and an Angular 2 app with NativeScript is in the UI elements that you use. NativeScript apps do not use a browser and do not have a DOM; therefore, elements like <code><div></code> and <code><span></code> simply do not work.</p>
315
-
<p>No worries though, as NativeScript provides an <ahref="http://docs.nativescript.org/ui/ui-views">extensive suite of UI elements</a>, each of which are implemented with native iOS and Android controls. For instance, the <ahref="http://docs.nativescript.org/ui/ui-views#label"><code><label></code> control</a> our previous example used is actually rendered as a <ahref="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UILabel_Class/"><code>UILabel</code></a> on iOS and an <ahref="http://developer.android.com/reference/android/widget/TextView.html"><code>android.widget.TextView</code></a> on Android. The great thing about using NativeScript though, is that this native details are transparent to use as a developer. You type <code><label></code> and let NativeScript handle the rendering details.</p>
315
+
<p>No worries though, as NativeScript provides an <ahref="http://docs.nativescript.org/ui/ui-views">extensive suite of UI elements</a>, each of which are implemented with native iOS and Android controls. For instance, the <ahref="http://docs.nativescript.org/ui/ui-views#label"><code><Label></code> control</a> our previous example used is actually rendered as a <ahref="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UILabel_Class/"><code>UILabel</code></a> on iOS and an <ahref="http://developer.android.com/reference/android/widget/TextView.html"><code>android.widget.TextView</code></a> on Android. The great thing about using NativeScript though, is that this native details are transparent to use as a developer. You type <code><Label></code> and let NativeScript handle the rendering details.</p>
316
316
<p>Let’s return back to building Groceries. The first screen of Groceries is intended to be a login screen, so let’s replace the current <code><Label></code> with something that resembles a typical login screen in a mobile app.</p>
317
317
<h4class="exercise-start">
318
318
<b>Exercise</b>: Add UI elements to <code>app.component.ts</code>
@@ -343,7 +343,7 @@ <h4 class="exercise-start">
343
343
<li><code>keyboardType</code>: The type of keyboard to present to the user for input. <code>keyboardType="email"</code> shows a keyboard optimized for entering email addresses. NativeScript currently supports <ahref="http://docs.nativescript.org/ui/keyboard.html">five types of keyboards</a> for text fields.</li>
344
344
<li><code>autocorrect</code>: A boolean attribute that determines whether the mobile operating system should autocorrect user input. In the case of email address text fields, the autocorrect behavior is undesirable.</li>
345
345
<li><code>autocapitalizationType</code>: Determines how the operating system should autocapitalize user input. <code>autocapitalizationType="none"</code> turns autocapitalization off altogether. NativeScript supports <ahref="http://docs.nativescript.org/ApiReference/ui/enums/AutocapitalizationType/README.html">four autocapitalization types</a> on text fields.</li>
346
-
<li><code>secure</code>: A boolean attribute that determines whether the TextField's text should be masked, which is commonly done on password fields.</li>
346
+
<li><code>secure</code>: A boolean attribute that determines whether the text field’s text should be masked, which is commonly done on password fields.</li>
347
347
</ul>
348
348
</li>
349
349
<li><code><Button></code><ul>
@@ -372,7 +372,7 @@ <h4 class="exercise-start">
372
372
<b>Exercise</b>: Add a stack layout to the login screen
373
373
</h4>
374
374
375
-
<p>In<code>app.component.ts</code>, add a <code><StackLayout></code> element within your component’s <code>template</code> property. The full component should now look like this:</p>
375
+
<p>Open<code>app/app.component.ts</code>, and add a <code><StackLayout></code> element within your component’s <code>template</code> property. The full component should now look like this:</p>
376
376
<pre><codeclass="lang-JavaScript">@Component({
377
377
selector: "my-app",
378
378
template: `
@@ -401,15 +401,15 @@ <h4 class="exercise-start">
401
401
</ul>
402
402
</blockquote>
403
403
<h3id="global-css">Global CSS</h3>
404
-
<p>NativeScript uses a <ahref="http://docs.nativescript.org/styling">subset of CSS</a> to change the visual appearance of your app. You can use three mechanisms to add CSS properties to UI components: <ahref="http://docs.nativescript.org/styling#application-wide-css">application-wide CSS</a> (<code>app.css</code>), component-specific CSS, and an <ahref="http://docs.nativescript.org/styling#inline-css">inline <code>style</code> attribute</a>. In this section we’ll cover application-wide, or global CSS, and in the next section we’ll look at how to apply CSS rules to individual components.</p>
404
+
<p>NativeScript uses a <ahref="http://docs.nativescript.org/styling">subset of CSS</a> to change the visual appearance of your app. You can use three mechanisms to add CSS properties to UI components: <ahref="http://docs.nativescript.org/styling#application-wide-css">application-wide CSS</a>, component-specific CSS, and an <ahref="http://docs.nativescript.org/styling#inline-css">inline <code>style</code> attribute</a>. In this section we’ll cover application-wide, or global CSS, and in the next section we’ll look at how to apply CSS rules to individual components.</p>
405
405
<blockquote>
406
406
<p><strong>TIP</strong>: Although inline styles are great for quick testing—e.g. <code><StackLayout style="background-color: green;"></code>—you should avoid them in general because the <code>style</code> attributes tend to clutter up your templates, especially if you need to apply multiple rules.</p>
407
407
</blockquote>
408
408
<h4class="exercise-start">
409
409
<b>Exercise</b>: Create global styles
410
410
</h4>
411
411
412
-
<p>Open your app’s <code>app/app.css</code> file and paste in the following code:</p>
412
+
<p>Open your <code>app/app.css</code> file and paste in the following code:</p>
413
413
<pre><codeclass="lang-CSS">Page {
414
414
background-color: white;
415
415
font-size: 15;
@@ -422,6 +422,9 @@ <h4 class="exercise-start">
422
422
<divclass="exercise-end"></div>
423
423
424
424
<p>If you've done any web development before, the syntax should feel familiar here. You select two UI components by their tag names (Page and TextField), and then apply a handful of CSS rules as name/value pairs. NativeScript does not support all CSS properties because it is not possible to replicate some of them in native apps without causing performance issues. A <ahref="http://docs.nativescript.org/styling#supported-properties">full list of the CSS properties that are supported</a> are listed in the NativeScript docs.</p>
425
+
<blockquote>
426
+
<p><strong>NOTE</strong>: In NativeScript, a single <code><Page></code> UI element wraps the <code>template</code> of every page-level Angular component, which you’ll learn about when we introduce routing later in this guide. For now, just know that a <code><Page></code> exists as the parent of your <code>AppComponent</code>’s template, and that you can target that <code><Page></code> with CSS as you would with any other UI element.</p>
427
+
</blockquote>
425
428
<p>Although often you want CSS rules to apply equally to your iOS and Android app, occasionally it makes sense to apply a CSS rule to only one platform. For example, iOS text fields frequently have borders around them, but Android text fields do not. Let's look at how to make platform-specific style changes in NativeScript.</p>
Copy file name to clipboardExpand all lines: src/chapters/chapter2.md
+8-6Lines changed: 8 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -118,13 +118,13 @@ Notice the interesting way that the `Component` class is used—with the syntax
118
118
119
119
However, this syntax may look a bit odd if you come from a web development background. On the web, the `<label>` HTML element doesn’t have a `text` attribute, so why do we see it here? Let’s dive into this by looking at how NativeScript UI elements work.
120
120
121
-
> **NOTE**: Curious about the `@Component` decorator’s `selector` property? The property defines how a component can be used within another component’s template. For instance a component that defines its `selector` with `selector: "foo-bar"` can be used by another component as `template: "<foo-bar></foo-bar>"`. NativeScript is smart enough to use your first Angular 2 component automatically; therefore, the `selector` property is currently irrelevant. We’ll use the `selector` property later in this guide though.
121
+
> **NOTE**: Curious about the `@Component` decorator’s `selector` property? The property defines how a component can be used within another component’s template. For instance a component that defines its `selector` with `selector: "foo-bar"` can be used by another component as `template: "<foo-bar></foo-bar>"`. NativeScript is smart enough to use your first Angular 2 component automatically; therefore, the `selector` property of this first component is irrelevant. We’ll use the `selector` property later in this guide though.
122
122
123
123
### Adding UI elements
124
124
125
125
The primary difference between building an Angular 2 app for the web and an Angular 2 app with NativeScript is in the UI elements that you use. NativeScript apps do not use a browser and do not have a DOM; therefore, elements like `<div>` and `<span>` simply do not work.
126
126
127
-
No worries though, as NativeScript provides an [extensive suite of UI elements](http://docs.nativescript.org/ui/ui-views), each of which are implemented with native iOS and Android controls. For instance, the [`<label>` control](http://docs.nativescript.org/ui/ui-views#label) our previous example used is actually rendered as a [`UILabel`](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UILabel_Class/) on iOS and an [`android.widget.TextView`](http://developer.android.com/reference/android/widget/TextView.html) on Android. The great thing about using NativeScript though, is that this native details are transparent to use as a developer. You type `<label>` and let NativeScript handle the rendering details.
127
+
No worries though, as NativeScript provides an [extensive suite of UI elements](http://docs.nativescript.org/ui/ui-views), each of which are implemented with native iOS and Android controls. For instance, the [`<Label>` control](http://docs.nativescript.org/ui/ui-views#label) our previous example used is actually rendered as a [`UILabel`](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UILabel_Class/) on iOS and an [`android.widget.TextView`](http://developer.android.com/reference/android/widget/TextView.html) on Android. The great thing about using NativeScript though, is that this native details are transparent to use as a developer. You type `<Label>` and let NativeScript handle the rendering details.
128
128
129
129
Let’s return back to building Groceries. The first screen of Groceries is intended to be a login screen, so let’s replace the current `<Label>` with something that resembles a typical login screen in a mobile app.
130
130
@@ -159,7 +159,7 @@ This code adds two new NativeScript UI elements: a [text field](http://docs.nati
159
159
-`keyboardType`: The type of keyboard to present to the user for input. `keyboardType="email"` shows a keyboard optimized for entering email addresses. NativeScript currently supports [five types of keyboards](http://docs.nativescript.org/ui/keyboard.html) for text fields.
160
160
-`autocorrect`: A boolean attribute that determines whether the mobile operating system should autocorrect user input. In the case of email address text fields, the autocorrect behavior is undesirable.
161
161
-`autocapitalizationType`: Determines how the operating system should autocapitalize user input. `autocapitalizationType="none"` turns autocapitalization off altogether. NativeScript supports [four autocapitalization types](http://docs.nativescript.org/ApiReference/ui/enums/AutocapitalizationType/README.html) on text fields.
162
-
-`secure`: A boolean attribute that determines whether the TextField's text should be masked, which is commonly done on password fields.
162
+
-`secure`: A boolean attribute that determines whether the text field’s text should be masked, which is commonly done on password fields.
163
163
-`<Button>`
164
164
-`text`: Controls the text displayed within the button.
165
165
@@ -188,7 +188,7 @@ For your login screen, all you need is a simple `<StackLayout>` for stacking the
188
188
<b>Exercise</b>: Add a stack layout to the login screen
189
189
</h4>
190
190
191
-
In`app.component.ts`, add a `<StackLayout>` element within your component’s `template` property. The full component should now look like this:
191
+
Open`app/app.component.ts`, and add a `<StackLayout>` element within your component’s `template` property. The full component should now look like this:
192
192
193
193
```JavaScript
194
194
@Component({
@@ -221,15 +221,15 @@ Although the UI elements are in the correct order, they could use some spacing a
221
221
222
222
### Global CSS
223
223
224
-
NativeScript uses a [subset of CSS](http://docs.nativescript.org/styling) to change the visual appearance of your app. You can use three mechanisms to add CSS properties to UI components: [application-wide CSS](http://docs.nativescript.org/styling#application-wide-css) (`app.css`), component-specific CSS, and an [inline `style` attribute](http://docs.nativescript.org/styling#inline-css). In this section we’ll cover application-wide, or global CSS, and in the next section we’ll look at how to apply CSS rules to individual components.
224
+
NativeScript uses a [subset of CSS](http://docs.nativescript.org/styling) to change the visual appearance of your app. You can use three mechanisms to add CSS properties to UI components: [application-wide CSS](http://docs.nativescript.org/styling#application-wide-css), component-specific CSS, and an [inline `style` attribute](http://docs.nativescript.org/styling#inline-css). In this section we’ll cover application-wide, or global CSS, and in the next section we’ll look at how to apply CSS rules to individual components.
225
225
226
226
> **TIP**: Although inline styles are great for quick testing—e.g. `<StackLayout style="background-color: green;">`—you should avoid them in general because the `style` attributes tend to clutter up your templates, especially if you need to apply multiple rules.
227
227
228
228
<h4class="exercise-start">
229
229
<b>Exercise</b>: Create global styles
230
230
</h4>
231
231
232
-
Open your app’s `app/app.css` file and paste in the following code:
232
+
Open your `app/app.css` file and paste in the following code:
233
233
234
234
```CSS
235
235
Page {
@@ -246,6 +246,8 @@ TextField {
246
246
247
247
If you've done any web development before, the syntax should feel familiar here. You select two UI components by their tag names (Page and TextField), and then apply a handful of CSS rules as name/value pairs. NativeScript does not support all CSS properties because it is not possible to replicate some of them in native apps without causing performance issues. A [full list of the CSS properties that are supported](http://docs.nativescript.org/styling#supported-properties) are listed in the NativeScript docs.
248
248
249
+
> **NOTE**: In NativeScript, a single `<Page>` UI element wraps the `template` of every page-level Angular component, which you’ll learn about when we introduce routing later in this guide. For now, just know that a `<Page>` exists as the parent of your `AppComponent`’s template, and that you can target that `<Page>` with CSS as you would with any other UI element.
250
+
249
251
Although often you want CSS rules to apply equally to your iOS and Android app, occasionally it makes sense to apply a CSS rule to only one platform. For example, iOS text fields frequently have borders around them, but Android text fields do not. Let's look at how to make platform-specific style changes in NativeScript.
0 commit comments