_id
stringlengths
21
254
text
stringlengths
1
93.7k
metadata
dict
angular/adev/src/content/tutorials/learn-angular/steps/7-event-handling/src/app/app.component.ts_0_297
import {Component} from '@angular/core'; @Component({ selector: 'app-root', template: ` <section> There's a secret message for you, hover to reveal 👀 {{ message }} </section> `, standalone: true, }) export class AppComponent { message = ''; onMouseOver() {} }
{ "end_byte": 297, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/7-event-handling/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/17-reactive-forms/README.md_0_3641
# Reactive Forms When you want to manage your forms programmatically instead of relying purely on the template, reactive forms are the answer. In this activity, you'll learn how to setup reactive forms. <hr> <docs-workflow> <docs-step title="Import `ReactiveForms` module"> In `app.component.ts`, import `ReactiveFormsModule` from `@angular/forms` and add it to the `imports` array of the component. ```angular-ts import { ReactiveFormsModule } from '@angular/forms'; @Component({ selector: 'app-root', standalone: true, template: ` <form> <label>Name <input type="text" /> </label> <label>Email <input type="email" /> </label> <button type="submit">Submit</button> </form> `, imports: [ReactiveFormsModule], }) ``` </docs-step> <docs-step title="Create the `FormGroup` object with FormControls"> Reactive forms use the `FormControl` class to represent the form controls (e.g., inputs). Angular provides the `FormGroup` class to serve as a grouping of form controls into a helpful object that makes handling large forms more convenient for developers. Add `FormControl` and `FormGroup` to the import from `@angular/forms` so that you can create a FormGroup for each form, with the properties `name` and `email` as FormControls. ```ts import {ReactiveFormsModule, FormControl, FormGroup } from '@angular/forms'; ... export class AppComponent { profileForm = new FormGroup({ name: new FormControl(''), email: new FormControl(''), }); } ``` </docs-step> <docs-step title="Link the FormGroup and FormControls to the form"> Each `FormGroup` should be attached to a form using the `[formGroup]` directive. In addition, each `FormControl` can be attached with the `formControlName` directive and assigned to the corresponding property. Update the template with the following form code: ```angular-html <form [formGroup]="profileForm"> <label> Name <input type="text" formControlName="name" /> </label> <label> Email <input type="email" formControlName="email" /> </label> <button type="submit">Submit</button> </form> ``` </docs-step> <docs-step title="Handle update to the form"> When you want to access data from the `FormGroup`, it can be done by accessing the value of the `FormGroup`. Update the `template` to display the form values: ```angular-html ... <h2>Profile Form</h2> <p>Name: {{ profileForm.value.name }}</p> <p>Email: {{ profileForm.value.email }}</p> ``` </docs-step> <docs-step title="Access FormGroup values"> Add a new method to the component class called `handleSubmit` that you'll later use to handle the form submission. This method will display values from the form, you can access the values from the FormGroup. In the component class, add the `handleSubmit()` method to handle the form submission. <docs-code language="ts"> handleSubmit() { alert( this.profileForm.value.name + ' | ' + this.profileForm.value.email ); } </docs-code> </docs-step> <docs-step title="Add `ngSubmit` to the form"> You have access to the form values, now it is time to handle the submission event and use the `handleSubmit` method. Angular has an event handler for this specific purpose called `ngSubmit`. Update the form element to call the `handleSubmit` method when the form is submitted. <docs-code language="angular-html" highlight="[3]"> <form [formGroup]="profileForm" (ngSubmit)="handleSubmit()"> </docs-code> </docs-step> </docs-workflow> And just like that, you know how to work with reactive forms in Angular. Fantastic job with this activity. Keep going to learn about form validation.
{ "end_byte": 3641, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/17-reactive-forms/README.md" }
angular/adev/src/content/tutorials/learn-angular/steps/17-reactive-forms/answer/src/app/app.component.ts_0_842
import {Component} from '@angular/core'; import {FormGroup, FormControl} from '@angular/forms'; import {ReactiveFormsModule} from '@angular/forms'; @Component({ selector: 'app-root', template: ` <form [formGroup]="profileForm" (ngSubmit)="handleSubmit()"> <input type="text" formControlName="name" /> <input type="email" formControlName="email" /> <button type="submit">Submit</button> </form> <h2>Profile Form</h2> <p>Name: {{ profileForm.value.name }}</p> <p>Email: {{ profileForm.value.email }}</p> `, standalone: true, imports: [ReactiveFormsModule], }) export class AppComponent { profileForm = new FormGroup({ name: new FormControl(''), email: new FormControl(''), }); handleSubmit() { alert(this.profileForm.value.name + ' | ' + this.profileForm.value.email); } }
{ "end_byte": 842, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/17-reactive-forms/answer/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/17-reactive-forms/src/app/app.component.ts_0_425
import {Component} from '@angular/core'; @Component({ selector: 'app-root', template: ` <form> <label> Name <input type="text" formControlName="name" /> </label> <label> Email <input type="email" formControlName="email" /> </label> <button type="submit">Submit</button> </form> `, standalone: true, imports: [], }) export class AppComponent {}
{ "end_byte": 425, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/17-reactive-forms/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/24-create-a-pipe/README.md_0_2154
# Create a custom pipe You can create custom pipes in Angular to fit your data transformation needs. In this activity, you will create a custom pipe and use it in your template. <hr> A pipe is a TypeScript class with a `@Pipe` decorator. Here's an example: ```ts import {Pipe, PipeTransform} from '@angular/core'; @Pipe({ standalone: true, name: 'star', }) export class StarPipe implements PipeTransform { transform(value: string): string { return `⭐️ ${value} ⭐️`; } } ``` The `StarPipe` accepts a string value and returns that string with stars around it. Take note that: - the name in the `@Pipe` decorator configuration is what will be used in the template - the `transform` function is where you put your logic Alright, it's your turn to give this a try — you'll create the `ReversePipe`: <docs-workflow> <docs-step title="Create the `ReversePipe`"> In `reverse.pipe.ts` add the `@Pipe` decorator to the `ReversePipe` class and provide the following configuration: ```ts @Pipe({ standalone: true, name: 'reverse' }) ``` </docs-step> <docs-step title="Implement the `transform` function"> Now the `ReversePipe` class is a pipe. Update the `transform` function to add the reversing logic: <docs-code language="ts" highlight="[3,4,5,6,7,8,9]"> export class ReversePipe implements PipeTransform { transform(value: string): string { let reverse = ''; for (let i = value.length - 1; i >= 0; i--) { reverse += value[i]; } return reverse; } } </docs-code> </docs-step> <docs-step title="Use the `ReversePipe` in the template"></docs-step> With the pipe logic implemented, the final step is to use it in the template. In `app.component.ts` include the pipe in the template and add it to the component imports: <docs-code language="angular-ts" highlight="[3,4]"> @Component({ ... template: `Reverse Machine: {{ word | reverse }}` imports: [ReversePipe] }) </docs-code> </docs-workflow> And with that you've done it. Congratulations on completing this activity. You now know how to use pipes and even how to implement your own custom pipes.
{ "end_byte": 2154, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/24-create-a-pipe/README.md" }
angular/adev/src/content/tutorials/learn-angular/steps/24-create-a-pipe/answer/src/app/app.component.ts_0_294
import {Component} from '@angular/core'; import {ReversePipe} from './reverse.pipe'; @Component({ selector: 'app-root', template: ` Reverse Machine: {{ word | reverse }} `, standalone: true, imports: [ReversePipe], }) export class AppComponent { word = 'You are a champion'; }
{ "end_byte": 294, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/24-create-a-pipe/answer/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/24-create-a-pipe/answer/src/app/reverse.pipe.ts_0_324
import {Pipe, PipeTransform} from '@angular/core'; @Pipe({ name: 'reverse', standalone: true, }) export class ReversePipe implements PipeTransform { transform(value: string): string { let reverse = ''; for (let i = value.length - 1; i >= 0; i--) { reverse += value[i]; } return reverse; } }
{ "end_byte": 324, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/24-create-a-pipe/answer/src/app/reverse.pipe.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/24-create-a-pipe/src/app/app.component.ts_0_273
import {Component} from '@angular/core'; import {ReversePipe} from './reverse.pipe'; @Component({ selector: 'app-root', template: ` Reverse Machine: {{ word }} `, standalone: true, imports: [], }) export class AppComponent { word = 'You are a champion'; }
{ "end_byte": 273, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/24-create-a-pipe/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/24-create-a-pipe/src/app/reverse.pipe.ts_0_162
import {Pipe, PipeTransform} from '@angular/core'; export class ReversePipe implements PipeTransform { transform(value: string): string { return ''; } }
{ "end_byte": 162, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/24-create-a-pipe/src/app/reverse.pipe.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/5-control-flow-for/README.md_0_1829
# Control Flow in Components - `@for` Often when building web applications, you need to repeat some code a specific number of times - for example, given an array of names, you may want to display each name in a `<p>` tag. In this activity, you'll learn how to use `@for` to repeat elements in a template. <hr/> The syntax that enables repeating elements in a template is `@for`. Here's an example of how to use the `@for` syntax in a component: ```angular-ts @Component({ ... template: ` @for (os of operatingSystems; track os.id) { {{ os.name }} } `, }) export class AppComponent { operatingSystems = [{id: 'win', name: 'Windows'}, {id: 'osx', name: 'MacOS'}, {id: 'linux', name: 'Linux'}]; } ``` Two things to take note of: * There is an `@` prefix for the `for` because it is a special syntax called [Angular template syntax](guide/templates) * For applications using v16 and older please refer to the [Angular documentation for NgFor](guide/directives/structural-directives) <docs-workflow> <docs-step title="Add the `users` property"> In the `AppComponent` class, add a property called `users` that contains users and their names. ```ts [{id: 0, name: 'Sarah'}, {id: 1, name: 'Amy'}, {id: 2, name: 'Rachel'}, {id: 3, name: 'Jessica'}, {id: 4, name: 'Poornima'}] ``` </docs-step> <docs-step title="Update the template"> Update the template to display each user name in a `p` element using the `@for` template syntax. ```angular-html @for (user of users; track user.id) { <p>{{ user.name }}</p> } ``` Note: the use of `track` is required, you may use the `id` or some other unique identifier. </docs-step> </docs-workflow> This type of functionality is called control flow. Next, you'll learn to customize and communicate with components - by the way, you're doing a great job so far.
{ "end_byte": 1829, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/5-control-flow-for/README.md" }
angular/adev/src/content/tutorials/learn-angular/steps/5-control-flow-for/answer/src/app/app.component.ts_0_386
import {Component} from '@angular/core'; @Component({ selector: 'app-root', template: ` @for(user of users; track user.id) { <p>{{ user.name }}</p> } `, standalone: true, }) export class AppComponent { users = [ {id: 0, name: 'Sarah'}, {id: 1, name: 'Amy'}, {id: 2, name: 'Rachel'}, {id: 3, name: 'Jessica'}, {id: 4, name: 'Poornima'}, ]; }
{ "end_byte": 386, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/5-control-flow-for/answer/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/5-control-flow-for/src/app/app.component.ts_0_147
import {Component} from '@angular/core'; @Component({ selector: 'app-root', template: ``, standalone: true, }) export class AppComponent {}
{ "end_byte": 147, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/5-control-flow-for/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/19-creating-an-injectable-service/README.md_0_2013
# Creating an injectable service Dependency injection (DI) in Angular is one of the framework's most powerful features. Consider dependency injection to be the ability for Angular to _provide_ resources you need for your application at runtime. A dependency could be a service or some other resources. You can learn more about [dependency injection in the Angular docs](guide/di). For now, you will get practice creating `injectable` resources. In this activity, you'll learn how to create an injectable service. <hr> One way to use a service is to act as a way to interact with data and APIs. To make a service reusable you should keep the logic in the service and share it throughout the application when it is needed. To make a service eligible to be injected by the DI system use the `@Injectable` decorator. For example: <docs-code language="ts" highlight="[1, 2, 3]"> @Injectable({ providedIn: 'root' }) class UserService { // methods to retrieve and return data } </docs-code> The `@Injectable` decorator notifies the DI system that the `UserService` is available to be requested in a class. `providedIn` sets the scope in which this resource is available. For now, it is good enough to understand that `providedIn: 'root'` means that the `UserService` is available to the entire application. Alright, you try: <docs-workflow> <docs-step title="Add the `@Injectable` decorator"> Update the code in `car.service.ts` by adding the `@Injectable` decorator. </docs-step> <docs-step title="Configure the decorator"> The values in the object passed to the decorator are considered to be the configuration for the decorator. <br> Update the `@Injectable` decorator in `car.service.ts` to include the configuration for `providedIn: 'root'`. Tip: Use the above example to find the correct syntax. </docs-step> </docs-workflow> Well, done 👍 that service is now `injectable` and can participate in the fun. Now that the service is `injectable`, let's try injecting it into a component 👉
{ "end_byte": 2013, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/19-creating-an-injectable-service/README.md" }
angular/adev/src/content/tutorials/learn-angular/steps/19-creating-an-injectable-service/answer/src/app/car.service.ts_0_277
import {Injectable} from '@angular/core'; @Injectable({ providedIn: 'root', }) export class CarService { cars = ['Sunflower GT', 'Flexus Sport', 'Sprout Mach One']; getCars(): string[] { return this.cars; } getCar(id: number) { return this.cars[id]; } }
{ "end_byte": 277, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/19-creating-an-injectable-service/answer/src/app/car.service.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/19-creating-an-injectable-service/answer/src/app/app.component.ts_0_268
import {Component, inject} from '@angular/core'; import {CarService} from './car.service'; @Component({ selector: 'app-root', template: '<p> {{ carService.getCars() }} </p>', standalone: true, }) export class AppComponent { carService = inject(CarService); }
{ "end_byte": 268, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/19-creating-an-injectable-service/answer/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/19-creating-an-injectable-service/src/app/car.service.ts_0_238
import {Injectable} from '@angular/core'; export class CarService { cars = ['Sunflower GT', 'Flexus Sport', 'Sprout Mach One']; getCars(): string[] { return this.cars; } getCar(id: number) { return this.cars[id]; } }
{ "end_byte": 238, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/19-creating-an-injectable-service/src/app/car.service.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/19-creating-an-injectable-service/src/app/app.component.ts_0_268
import {Component, inject} from '@angular/core'; import {CarService} from './car.service'; @Component({ selector: 'app-root', template: '<p> {{ carService.getCars() }} </p>', standalone: true, }) export class AppComponent { carService = inject(CarService); }
{ "end_byte": 268, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/19-creating-an-injectable-service/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/23-pipes-format-data/README.md_0_2126
# Formatting data with pipes You can take your use of pipes even further by configuring them. Pipes can be configured by passing options to them. In this activity, you will work with some pipes and pipe parameters. <hr> To pass parameters to a pipe, use the `:` syntax followed by the parameter value. Here's an example: ```ts template: `{{ date | date:'medium' }}`; ``` The output is `Jun 15, 2015, 9:43:11 PM`. Time to customize some pipe output: <docs-workflow> <docs-step title="Format a number with `DecimalPipe`"> In `app.component.ts`, update the template to include parameter for the `decimal` pipe. <docs-code language="ts" highlight="[3]"> template: ` ... <li>Number with "decimal" {{ num | number:"3.2-2" }}</li> ` </docs-code> Note: What's that format? The parameter for the `DecimalPipe` is called `digitsInfo`, this parameter uses the format: `{minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}` </docs-step> <docs-step title="Format a date with `DatePipe`"> Now, update the template to use the `date` pipe. <docs-code language="ts" highlight="[3]"> template: ` ... <li>Date with "date" {{ birthday | date: 'medium' }}</li> ` </docs-code> For extra fun, try some different parameters for `date`. More information can be found in the [Angular docs](guide/templates/pipes). </docs-step> <docs-step title="Format a currency with `CurrencyPipe`"> For your last task, update the template to use the `currency` pipe. <docs-code language="ts" highlight="[3]"> template: ` ... <li>Currency with "currency" {{ cost | currency }}</li> ` </docs-code> You can also try different parameters for `currency`. More information can be found in the [Angular docs](guide/templates/pipes). </docs-step> </docs-workflow> Great work with pipes. You've made some great progress so far. There are even more built-in pipes that you can use in your applications. You can find the list in the [Angular documentation](guide/templates/pipes). In the case that the built-in pipes don't cover your needs, you can also create a custom pipe. Check out the next lesson to find out more.
{ "end_byte": 2126, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/23-pipes-format-data/README.md" }
angular/adev/src/content/tutorials/learn-angular/steps/23-pipes-format-data/answer/src/app/app.component.ts_0_553
import {Component} from '@angular/core'; import {DecimalPipe, DatePipe, CurrencyPipe} from '@angular/common'; @Component({ selector: 'app-root', template: ` <ul> <li>Number with "decimal" {{ num | number : '3.2-2' }}</li> <li>Date with "date" {{ birthday | date : 'medium' }}</li> <li>Currency with "currency" {{ cost | currency }}</li> </ul> `, standalone: true, imports: [DecimalPipe, DatePipe, CurrencyPipe], }) export class AppComponent { num = 103.1234; birthday = new Date(2023, 3, 2); cost = 4560.34; }
{ "end_byte": 553, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/23-pipes-format-data/answer/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/23-pipes-format-data/src/app/app.component.ts_0_505
import {Component} from '@angular/core'; import {DecimalPipe, DatePipe, CurrencyPipe} from '@angular/common'; @Component({ selector: 'app-root', template: ` <ul> <li>Number with "decimal" {{ num }}</li> <li>Date with "date" {{ birthday }}</li> <li>Currency with "currency" {{ cost }}</li> </ul> `, standalone: true, imports: [DecimalPipe, DatePipe, CurrencyPipe], }) export class AppComponent { num = 103.1234; birthday = new Date(2023, 3, 2); cost = 4560.34; }
{ "end_byte": 505, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/23-pipes-format-data/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/13-define-a-route/README.md_0_1823
# Define a Route Now that you've set up the app to use Angular Router, you need to define the routes. In this activity, you'll learn how to add and configure routes with your app. <hr> <docs-workflow> <docs-step title="Define a route in `app.routes.ts`"> In your app, there are two pages to display: (1) Home Page and (2) User Page. To define a route, add a route object to the `routes` array in `app.routes.ts` that contains: - The `path` of the route (which automatically starts at the root path (i.e., `/`)) - The `component` that you want the route to display ```ts import {Routes} from '@angular/router'; import {HomeComponent} from './home/home.component'; export const routes: Routes = [ { path: '', component: HomeComponent, }, ]; ``` The code above is an example of how `HomeComponent` can be added as a route. Now go ahead and implement this along with the `UserComponent` in the playground. Use `'user'` for the path of `UserComponent`. </docs-step> <docs-step title="Add title to route definition"> In addition to defining the routes correctly, Angular Router also enables you to set the page title whenever users are navigating by adding the `title` property to each route. In `app.routes.ts`, add the `title` property to the default route (`path: ''`) and the `user` route. Here's an example: <docs-code language="ts" highlight="[8]"> import {Routes} from '@angular/router'; import {HomeComponent} from './home/home.component'; export const routes: Routes = [ { path: '', title: 'App Home Page', component: HomeComponent, }, ]; </docs-code> </docs-step> </docs-workflow> In the activity, you've learned how to define and configure routes in your Angular app. Nice work. 🙌 The journey to fully enabling routing in your app is almost complete, keep going.
{ "end_byte": 1823, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/13-define-a-route/README.md" }
angular/adev/src/content/tutorials/learn-angular/steps/13-define-a-route/answer/src/main.ts_0_241
import {bootstrapApplication} from '@angular/platform-browser'; import {AppComponent} from './app/app.component'; import {appConfig} from './app/app.config'; bootstrapApplication(AppComponent, appConfig).catch((err) => console.error(err));
{ "end_byte": 241, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/13-define-a-route/answer/src/main.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/13-define-a-route/answer/src/app/app.routes.ts_0_349
import {Routes} from '@angular/router'; import {HomeComponent} from './home/home.component'; import {UserComponent} from './user/user.component'; export const routes: Routes = [ { path: '', title: 'App Home Page', component: HomeComponent, }, { path: 'user', title: 'App User Page', component: UserComponent, }, ];
{ "end_byte": 349, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/13-define-a-route/answer/src/app/app.routes.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/13-define-a-route/answer/src/app/app.component.ts_0_346
import {Component} from '@angular/core'; import {RouterOutlet} from '@angular/router'; @Component({ selector: 'app-root', template: ` <nav> <a href="/">Home</a> | <a href="/user">User</a> </nav> <router-outlet></router-outlet> `, standalone: true, imports: [RouterOutlet], }) export class AppComponent {}
{ "end_byte": 346, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/13-define-a-route/answer/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/13-define-a-route/answer/src/app/app.config.ts_0_222
import {ApplicationConfig} from '@angular/core'; import {provideRouter} from '@angular/router'; import {routes} from './app.routes'; export const appConfig: ApplicationConfig = { providers: [provideRouter(routes)], };
{ "end_byte": 222, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/13-define-a-route/answer/src/app/app.config.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/13-define-a-route/answer/src/app/home/home.component.ts_0_176
import {Component} from '@angular/core'; @Component({ selector: 'app-home', template: ` <div>Home Page</div> `, standalone: true, }) export class HomeComponent {}
{ "end_byte": 176, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/13-define-a-route/answer/src/app/home/home.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/13-define-a-route/answer/src/app/user/user.component.ts_0_218
import {Component} from '@angular/core'; @Component({ selector: 'app-user', template: ` <div>Username: {{ username }}</div> `, standalone: true, }) export class UserComponent { username = 'youngTech'; }
{ "end_byte": 218, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/13-define-a-route/answer/src/app/user/user.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/13-define-a-route/src/main.ts_0_241
import {bootstrapApplication} from '@angular/platform-browser'; import {AppComponent} from './app/app.component'; import {appConfig} from './app/app.config'; bootstrapApplication(AppComponent, appConfig).catch((err) => console.error(err));
{ "end_byte": 241, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/13-define-a-route/src/main.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/13-define-a-route/src/app/app.routes.ts_0_181
import {Routes} from '@angular/router'; import {HomeComponent} from './home/home.component'; import {UserComponent} from './user/user.component'; export const routes: Routes = [];
{ "end_byte": 181, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/13-define-a-route/src/app/app.routes.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/13-define-a-route/src/app/app.component.ts_0_346
import {Component} from '@angular/core'; import {RouterOutlet} from '@angular/router'; @Component({ selector: 'app-root', template: ` <nav> <a href="/">Home</a> | <a href="/user">User</a> </nav> <router-outlet></router-outlet> `, standalone: true, imports: [RouterOutlet], }) export class AppComponent {}
{ "end_byte": 346, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/13-define-a-route/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/13-define-a-route/src/app/app.config.ts_0_222
import {ApplicationConfig} from '@angular/core'; import {provideRouter} from '@angular/router'; import {routes} from './app.routes'; export const appConfig: ApplicationConfig = { providers: [provideRouter(routes)], };
{ "end_byte": 222, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/13-define-a-route/src/app/app.config.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/13-define-a-route/src/app/home/home.component.ts_0_176
import {Component} from '@angular/core'; @Component({ selector: 'app-home', template: ` <div>Home Page</div> `, standalone: true, }) export class HomeComponent {}
{ "end_byte": 176, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/13-define-a-route/src/app/home/home.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/13-define-a-route/src/app/user/user.component.ts_0_218
import {Component} from '@angular/core'; @Component({ selector: 'app-user', template: ` <div>Username: {{ username }}</div> `, standalone: true, }) export class UserComponent { username = 'youngTech'; }
{ "end_byte": 218, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/13-define-a-route/src/app/user/user.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/2-updating-the-component-class/README.md_0_1700
# Updating the Component Class In Angular, the component's logic and behavior are defined in the component's TypeScript class. In this activity, you'll learn how to update the component class and how to use [interpolation](/guide/templates/binding#render-dynamic-text-with-text-interpolation). <hr /> <docs-workflow> <docs-step title="Add a property called `city`"> Update the component class by adding a property called `city` to the `AppComponent` class. ```ts export class AppComponent { city = 'San Francisco'; } ``` The `city` property is of type `string` but you can omit the type because of [type inference in TypeScript](https://www.typescriptlang.org/docs/handbook/type-inference.html). The `city` property can be used in the `AppComponent` class and can be referenced in the component template. <br> To use a class property in a template, you have to use the `{{ }}` syntax. </docs-step> <docs-step title="Update the component template"> Update the `template` property to match the following HTML: ```ts template: `Hello {{ city }}`, ``` This is an example of interpolation and is a part of Angular template syntax. It enables you to do much more than put dynamic text in a template. You can also use this syntax to call functions, write expressions and more. </docs-step> <docs-step title="More practice with interpolation"> Try this - add another set of `{{ }}` with the contents being `1 + 1`: ```ts template: `Hello {{ city }}, {{ 1 + 1 }}`, ``` Angular evaluates the contents of the `{{ }}` and renders the output in the template. </docs-step> </docs-workflow> This is just the beginning of what's possible with Angular templates, keep on learning to find out more.
{ "end_byte": 1700, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/2-updating-the-component-class/README.md" }
angular/adev/src/content/tutorials/learn-angular/steps/2-updating-the-component-class/answer/src/app/app.component.ts_0_211
import {Component} from '@angular/core'; @Component({ selector: 'app-root', template: ` Hello {{ city }}, {{ 1 + 1 }} `, standalone: true, }) export class AppComponent { city = 'San Francisco'; }
{ "end_byte": 211, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/2-updating-the-component-class/answer/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/2-updating-the-component-class/src/app/app.component.ts_0_160
import {Component} from '@angular/core'; @Component({ selector: 'app-root', template: ` Hello `, standalone: true, }) export class AppComponent {}
{ "end_byte": 160, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/2-updating-the-component-class/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/1-components-in-angular/README.md_0_1109
# Components in Angular Components are the foundational building blocks for any Angular application. Each component has three parts: * TypeScript class * HTML template * CSS styles In this activity, you'll learn how to update the template and styles of a component. <hr /> This is a great opportunity for you to get started with Angular. <docs-workflow> <docs-step title="Update the component template"> Update the `template` property to read `Hello Universe` ```ts template: ` Hello Universe `, ``` When you changed the HTML template, the preview updated with your message. Let's go one step further: change the color of the text. </docs-step> <docs-step title="Update the component styles"> Update the styles value and change the `color` property from `blue` to `#a144eb`. ```ts styles: ` :host { color: #a144eb; } `, ``` When you check the preview, you'll find that the text color will be changed. </docs-step> </docs-workflow> In Angular, you can use all the browser supported CSS and HTML that's available. If you'd like, you can store your template and styles in separate files.
{ "end_byte": 1109, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/1-components-in-angular/README.md" }
angular/adev/src/content/tutorials/learn-angular/steps/1-components-in-angular/answer/src/app/app.component.ts_0_226
import {Component} from '@angular/core'; @Component({ selector: 'app-root', template: ` Hello Universe `, styles: ` :host { color: #a144eb; } `, standalone: true, }) export class AppComponent {}
{ "end_byte": 226, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/1-components-in-angular/answer/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/1-components-in-angular/src/app/app.component.ts_0_214
import {Component} from '@angular/core'; @Component({ selector: 'app-root', template: ` Hello `, styles: ` :host { color: blue; } `, standalone: true, }) export class AppComponent {}
{ "end_byte": 214, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/1-components-in-angular/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/9-output/README.md_0_2365
# Component Communication with `@Output` When working with components it may be required to notify other components that something has happened. Perhaps a button has been clicked, an item has been added/removed from a list or some other important update has occurred. In this scenario components need to communicate with parent components. Angular uses the `@Output` decorator to enable this type of behavior. In this activity, you'll learn how to use the `@Output` decorator and `EventEmitter` to communicate with components. <hr /> To create the communication path from child to parent components, use the `@Output` decorator on a class property and assign it a value of type `EventEmitter`: <docs-code header="child.component.ts" language="ts"> @Component({...}) class ChildComponent { @Output() incrementCountEvent = new EventEmitter<number>(); } </docs-code> Now the component can generate events that can be listened to by the parent component. Trigger events by calling the `emit` method: <docs-code header="child.component.ts" language="ts"> class ChildComponent { ... onClick() { this.count++; this.incrementCountEvent.emit(this.count); } } </docs-code> The emit function will generate an event with the same type as the `EventEmitter` instance. Alright, your turn to give this a try. Complete the code by following these tasks: <docs-workflow> <docs-step title="Add an `@Output` property"> Update `child.component.ts` by adding an output property called `addItemEvent`, be sure to set the EventEmitter type to be `string`. </docs-step> <docs-step title="Complete `addItem` method"> In `child.component.ts` update the `addItem` method; use the following code as the logic: <docs-code header="child.component.ts" highlight="[2]" language="ts"> addItem() { this.addItemEvent.emit('🐢'); } </docs-code> </docs-step> <docs-step title="Update the `AppComponent` template"> In `app.component.ts` update the template to listen to the emitted event by adding the following code: ```angular-html <app-child (addItemEvent)="addItem($event)" /> ``` Now, the "Add Item" button adds a new item to the list every time the button is clicked. </docs-step> </docs-workflow> Wow, at this point you've completed the component fundamentals - impressive 👏 Keep learning to unlock more of Angular's great features.
{ "end_byte": 2365, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/9-output/README.md" }
angular/adev/src/content/tutorials/learn-angular/steps/9-output/answer/src/app/app.component.ts_0_414
import {Component} from '@angular/core'; import {ChildComponent} from './child.component'; @Component({ selector: 'app-root', template: ` <app-child (addItemEvent)="addItem($event)" /> <p>🐢 all the way down {{ items.length }}</p> `, standalone: true, imports: [ChildComponent], }) export class AppComponent { items = new Array(); addItem(item: string) { this.items.push(item); } }
{ "end_byte": 414, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/9-output/answer/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/9-output/answer/src/app/child.component.ts_0_384
import {Component, Output, EventEmitter} from '@angular/core'; @Component({ selector: 'app-child', styles: `.btn { padding: 5px; }`, template: ` <button class="btn" (click)="addItem()">Add Item</button> `, standalone: true, }) export class ChildComponent { @Output() addItemEvent = new EventEmitter<string>(); addItem() { this.addItemEvent.emit('🐢'); } }
{ "end_byte": 384, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/9-output/answer/src/app/child.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/9-output/src/app/app.component.ts_0_381
import {Component} from '@angular/core'; import {ChildComponent} from './child.component'; @Component({ selector: 'app-root', template: ` <app-child /> <p>🐢 all the way down {{ items.length }}</p> `, standalone: true, imports: [ChildComponent], }) export class AppComponent { items = new Array(); addItem(item: string) { this.items.push(item); } }
{ "end_byte": 381, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/9-output/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/9-output/src/app/child.component.ts_0_289
import {Component, Output, EventEmitter} from '@angular/core'; @Component({ selector: 'app-child', styles: `.btn { padding: 5px; }`, template: ` <button class="btn" (click)="addItem()">Add Item</button> `, standalone: true, }) export class ChildComponent { addItem() {} }
{ "end_byte": 289, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/9-output/src/app/child.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/6-property-binding/README.md_0_1752
# Property Binding in Angular Property binding in Angular enables you to set values for properties of HTML elements, Angular components and more. Use property binding to dynamically set values for properties and attributes. You can do things such as toggle button features, set image paths programmatically, and share values between components. In this activity, you'll learn how to use property binding in templates. <hr /> To bind to an element's attribute, wrap the attribute name in square brackets. Here's an example: ```angular-html <img alt="photo" [src]="imageURL"> ``` In this example, the value of the `src` attribute will be bound to the class property `imageURL`. Whatever value `imageURL` has will be set as the `src` attribute of the `img` tag. <docs-workflow> <docs-step title="Add a property called `isEditable`" header="app.component.ts" language="ts"> Update the code in `app.component.ts` by adding a property to the `AppComponent` class called `isEditable` with the initial value set to `true`. <docs-code highlight="[2]"> export class AppComponent { isEditable = true; } </docs-code> </docs-step> <docs-step title="Bind to `contentEditable`" header="app.component.ts" language="ts"> Next, bind the `contentEditable` attribute of the `div` to the `isEditable` property by using the <code aria-label="square brackets">[]</code> syntax. <docs-code highlight="[3]" language="angular-ts"> @Component({ ... template: `<div [contentEditable]="isEditable"></div>`, }) </docs-code> </docs-step> </docs-workflow> The div is now editable. Nice work 👍 Property binding is one of Angular's many powerful features. If you'd like to learn more checkout [the Angular documentation](guide/templates/property-binding).
{ "end_byte": 1752, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/6-property-binding/README.md" }
angular/adev/src/content/tutorials/learn-angular/steps/6-property-binding/answer/src/app/app.component.ts_0_255
import {Component} from '@angular/core'; @Component({ selector: 'app-root', styleUrls: ['app.component.css'], template: ` <div [contentEditable]="isEditable"></div> `, standalone: true, }) export class AppComponent { isEditable = true; }
{ "end_byte": 255, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/6-property-binding/answer/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/6-property-binding/answer/src/app/app.component.css_0_98
div { border: solid 2px #a144eb; padding: 5px; height: 20px; border-radius: 4px; }
{ "end_byte": 98, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/6-property-binding/answer/src/app/app.component.css" }
angular/adev/src/content/tutorials/learn-angular/steps/6-property-binding/src/app/app.component.ts_0_226
import {Component} from '@angular/core'; @Component({ selector: 'app-root', styleUrls: ['app.component.css'], template: ` <div contentEditable="false"></div> `, standalone: true, }) export class AppComponent {}
{ "end_byte": 226, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/6-property-binding/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/6-property-binding/src/app/app.component.css_0_98
div { border: solid 2px #a144eb; padding: 5px; height: 20px; border-radius: 4px; }
{ "end_byte": 98, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/6-property-binding/src/app/app.component.css" }
angular/adev/src/content/tutorials/learn-angular/steps/22-pipes/README.md_0_1655
# Pipes Pipes are functions that are used to transform data in templates. In general, pipes are "pure" functions that don't cause side effects. Angular has a number of helpful built-in pipes you can import and use in your components. You can also create a custom pipe. In this activity, you will import a pipe and use it in the template. <hr> To use a pipe in a template include it in an interpolated expression. Check out this example: <docs-code language="angular-ts" highlight="[1,5,6]"> import {UpperCasePipe} from '@angular/common'; @Component({ ... template: `{{ loudMessage | uppercase }}`, imports: [UpperCasePipe], }) class AppComponent { loudMessage = 'we think you are doing great!' } </docs-code> Now, it's your turn to give this a try: <docs-workflow> <docs-step title="Import the `LowerCase` pipe"> First, update `app.component.ts` by adding the file level import for `LowerCasePipe` from `@angular/common`. ```ts import { LowerCasePipe } from '@angular/common'; ``` </docs-step> <docs-step title="Add the pipe to the template imports"> Next, update `@Component()` decorator `imports` to include a reference to `LowerCasePipe` <docs-code language="ts" highlight="[3]"> @Component({ ... imports: [LowerCasePipe] }) </docs-code> </docs-step> <docs-step title="Add the pipe to the template"> Finally, in `app.component.ts` update the template to include the `lowercase` pipe: ```ts template: `{{username | lowercase }}` ``` </docs-step> </docs-workflow> Pipes can also accept parameters which can be used to configure their output. Find out more in the next activity. P.S. you are doing great ⭐️
{ "end_byte": 1655, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/22-pipes/README.md" }
angular/adev/src/content/tutorials/learn-angular/steps/22-pipes/answer/src/app/app.component.ts_0_283
import {Component} from '@angular/core'; import {LowerCasePipe} from '@angular/common'; @Component({ selector: 'app-root', template: ` {{ username | lowercase }} `, standalone: true, imports: [LowerCasePipe], }) export class AppComponent { username = 'yOunGTECh'; }
{ "end_byte": 283, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/22-pipes/answer/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/22-pipes/src/app/app.component.ts_0_211
import {Component} from '@angular/core'; @Component({ selector: 'app-root', template: ` {{ username }} `, standalone: true, imports: [], }) export class AppComponent { username = 'yOunGTECh'; }
{ "end_byte": 211, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/22-pipes/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/3-composing-components/README.md_0_1717
# Composing Components You've learned to update the component template, component logic, and component styles, but how do you use a component in your application? The `selector` property of the component configuration gives you a name to use when referencing the component in another template. You use the `selector` like an HTML tag, for example `app-user` would be `<app-user />` in the template. In this activity, you'll learn how to compose components. <hr/> In this example, there are two components `UserComponent` and `AppComponent`. <docs-workflow> <docs-step title="Add a reference to `UserComponent`"> Update the `AppComponent` template to include a reference to the `UserComponent` which uses the selector `app-user`. Be sure to add `UserComponent` to the imports array of `AppComponent`, this makes it available for use in the `AppComponent` template. ```ts template: `<app-user />`, imports: [UserComponent] ``` The component now displays the message `Username: youngTech`. You can update the template code to include more markup. </docs-step> <docs-step title="Add more markup"> Because you can use any HTML markup that you want in a template, try updating the template for `AppComponent` to also include more HTML elements. This example will add a `<section>` element as the parent of the `<app-user>` element. ```ts template: `<section><app-user /></section>`, ``` </docs-step> </docs-workflow> You can use as much HTML markup and as many components as you need to bring your app idea to reality. You can even have multiple copies of your component on the same page. That's a great segue, how would you conditionally show a component based on data? Head to the next section to find out.
{ "end_byte": 1717, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/3-composing-components/README.md" }
angular/adev/src/content/tutorials/learn-angular/steps/3-composing-components/answer/src/app/app.component.ts_0_392
import {Component} from '@angular/core'; @Component({ selector: 'app-user', template: ` Username: {{ username }} `, standalone: true, }) export class UserComponent { username = 'youngTech'; } @Component({ selector: 'app-root', template: ` <section> <app-user /> </section> `, standalone: true, imports: [UserComponent], }) export class AppComponent {}
{ "end_byte": 392, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/3-composing-components/answer/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/3-composing-components/src/app/app.component.ts_0_328
import {Component} from '@angular/core'; @Component({ selector: 'app-user', template: ` Username: {{ username }} `, standalone: true, }) export class UserComponent { username = 'youngTech'; } @Component({ selector: 'app-root', template: ``, standalone: true, imports: [], }) export class AppComponent {}
{ "end_byte": 328, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/3-composing-components/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/16-form-control-values/README.md_0_1726
# Getting form control value Now that your forms are setup with Angular, the next step is to access the values from the form controls. In this activity, you'll learn how to get the value from your form input. <hr> <docs-workflow> <docs-step title="Show the value of the input field in the template"> To display the input value in a template, you can use the interpolation syntax `{{}}` just like any other class property of the component: <docs-code language="angular-ts" highlight="[5]"> @Component({ selector: 'app-user', template: ` ... <p>Framework: {{ favoriteFramework }}</p> <label for="framework"> Favorite Framework: <input id="framework" type="text" [(ngModel)]="favoriteFramework" /> </label> `, }) export class UserComponent { favoriteFramework = ''; } </docs-code> </docs-step> <docs-step title="Retrieve the value of an input field"> When you need to reference the input field value in the component class, you can do so by accessing the class property with the `this` syntax. <docs-code language="angular-ts" highlight="[15]"> ... @Component({ selector: 'app-user', template: ` ... <button (click)="showFramework()">Show Framework</button> `, ... }) export class UserComponent { favoriteFramework = ''; ... showFramework() { alert(this.favoriteFramework); } } </docs-code> </docs-step> </docs-workflow> Great job learning how to display the input values in your template and access them programmatically. Time to progress onto the next way of managing forms with Angular: reactive forms. If you'd like to learn more about template-driven forms, please refer to the [Angular forms documentation](guide/forms/template-driven-forms).
{ "end_byte": 1726, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/16-form-control-values/README.md" }
angular/adev/src/content/tutorials/learn-angular/steps/16-form-control-values/answer/src/app/app.component.ts_0_243
import {Component} from '@angular/core'; import {UserComponent} from './user.component'; @Component({ selector: 'app-root', template: ` <app-user /> `, standalone: true, imports: [UserComponent], }) export class AppComponent {}
{ "end_byte": 243, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/16-form-control-values/answer/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/16-form-control-values/answer/src/app/user.component.ts_0_620
import {Component} from '@angular/core'; import {FormsModule} from '@angular/forms'; @Component({ selector: 'app-user', template: ` <p>Username: {{ username }}</p> <p>Framework: {{ favoriteFramework }}</p> <label for="framework"> Favorite Framework: <input id="framework" type="text" [(ngModel)]="favoriteFramework" /> </label> <button (click)="showFramework()">Show Framework</button> `, standalone: true, imports: [FormsModule], }) export class UserComponent { favoriteFramework = ''; username = 'youngTech'; showFramework() { alert(this.favoriteFramework); } }
{ "end_byte": 620, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/16-form-control-values/answer/src/app/user.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/16-form-control-values/src/app/app.component.ts_0_243
import {Component} from '@angular/core'; import {UserComponent} from './user.component'; @Component({ selector: 'app-root', template: ` <app-user /> `, standalone: true, imports: [UserComponent], }) export class AppComponent {}
{ "end_byte": 243, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/16-form-control-values/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/16-form-control-values/src/app/user.component.ts_0_558
import {Component} from '@angular/core'; import {FormsModule} from '@angular/forms'; @Component({ selector: 'app-user', template: ` <p>Username: {{ username }}</p> <p>Framework:</p> <label for="framework"> Favorite Framework: <input id="framework" type="text" [(ngModel)]="favoriteFramework" /> </label> <button (click)="showFramework()">Show Framework</button> `, standalone: true, imports: [FormsModule], }) export class UserComponent { favoriteFramework = ''; username = 'youngTech'; showFramework() {} }
{ "end_byte": 558, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/16-form-control-values/src/app/user.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/18-forms-validation/README.md_0_1963
# Validating forms Another common scenario when working with forms is the need to validate the inputs to ensure the correct data is submitted. In this activity, you'll learn how to validate forms with reactive forms. <hr> <docs-workflow> <docs-step title="Import Validators"> Angular provides a set of validation tools. To use them, first update the component to import `Validators` from `@angular/forms`. <docs-code language="ts" highlight="[1]"> import {ReactiveFormsModule, Validators} from '@angular/forms'; @Component({...}) export class AppComponent {} </docs-code> </docs-step> <docs-step title="Add validation to form"> Every `FormControl` can be passed the `Validators` you want to use for validating the `FormControl` values. For example, if you want to make the `name` field in `profileForm` required then use `Validators.required`. For the `email` field in our Angular form, we want to ensure it's not left empty and follows a valid email address structure. We can achieve this by combining the `Validators.required` and `Validators.email` validators in an array. Update the `name` and `email` `FormControl`: ```ts profileForm = new FormGroup({ name: new FormControl('', Validators.required), email: new FormControl('', [Validators.required, Validators.email]), }); ``` </docs-step> <docs-step title="Check form validation in template"> To determine if a form is valid, the `FormGroup` class has a `valid` property. You can use this property to dynamically bind attributes. Update the submit `button` to be enabled based on the validity of the form. ```angular-html <button type="submit" [disabled]="!profileForm.valid">Submit</button> ``` </docs-step> </docs-workflow> You now know the basics around how validation works with reactive forms. Great job learning these core concepts of working with forms in Angular. If you want to learn more, be sure to refer to the [Angular forms documentation](guide/forms/form-validation).
{ "end_byte": 1963, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/18-forms-validation/README.md" }
angular/adev/src/content/tutorials/learn-angular/steps/18-forms-validation/answer/src/app/app.component.ts_0_721
import {Component} from '@angular/core'; import {FormGroup, FormControl} from '@angular/forms'; import {ReactiveFormsModule, Validators} from '@angular/forms'; @Component({ selector: 'app-root', template: ` <form [formGroup]="profileForm"> <input type="text" formControlName="name" name="name" /> <input type="email" formControlName="email" name="email" /> <button type="submit" [disabled]="!profileForm.valid">Submit</button> </form> `, standalone: true, imports: [ReactiveFormsModule], }) export class AppComponent { profileForm = new FormGroup({ name: new FormControl('', Validators.required), email: new FormControl('', [Validators.required, Validators.email]), }); }
{ "end_byte": 721, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/18-forms-validation/answer/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/18-forms-validation/src/app/app.component.ts_0_615
import {Component} from '@angular/core'; import {FormGroup, FormControl} from '@angular/forms'; import {ReactiveFormsModule} from '@angular/forms'; @Component({ selector: 'app-root', template: ` <form [formGroup]="profileForm"> <input type="text" formControlName="name" name="name" /> <input type="email" formControlName="email" name="email" /> <button type="submit">Submit</button> </form> `, standalone: true, imports: [ReactiveFormsModule], }) export class AppComponent { profileForm = new FormGroup({ name: new FormControl(''), email: new FormControl(''), }); }
{ "end_byte": 615, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/18-forms-validation/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/15-forms/README.md_0_2228
# Forms Overview Forms are a big part of many apps because they enable your app to accept user input. Let's learn about how forms are handled in Angular. In Angular, there are two types of forms: template-driven and reactive. You'll learn about both over the next few activities. In this activity, you'll learn how to setup a form using a template-driven approach. <hr> <docs-workflow> <docs-step title="Create an input field"> In `user.component.ts`, update the template by adding a text input with the `id` set to `framework`, type set to `text`. ```angular-html <label for="framework"> Favorite Framework: <input id="framework" type="text" /> </label> ``` </docs-step> <docs-step title="Import `FormsModule`"> For this form to use Angular features that enable data binding to forms, you'll need to import the `FormsModule`. Import the `FormsModule` from `@angular/forms` and add it to the `imports` array of the `UserComponent`. <docs-code language="ts" highlight="[2, 7]"> import {Component} from '@angular/core'; import {FormsModule} from '@angular/forms'; @Component({ ... standalone: true, imports: [FormsModule], }) export class UserComponent {} </docs-code> </docs-step> <docs-step title="Add binding to the value of the input"> The `FormsModule` has a directive called `ngModel` that binds the value of the input to a property in your class. Update the input to use the `ngModel` directive, specifically with the following syntax `[(ngModel)]="favoriteFramework"` to bind to the `favoriteFramework` property. <docs-code language="html" highlight="[3]"> <label for="framework"> Favorite Framework: <input id="framework" type="text" [(ngModel)]="favoriteFramework" /> </label> </docs-code> After you've made changes, try entering a value in the input field. Notice how it updates on the screen (yes, very cool). Note: The syntax `[()]` is known as "banana in a box" but it represents two-way binding: property binding and event binding. Learn more in the [Angular docs about two-way data binding](guide/templates/two-way-binding). </docs-step> </docs-workflow> You've now taken an important first step towards building forms with Angular. Nice work. Let's keep the momentum going!
{ "end_byte": 2228, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/15-forms/README.md" }
angular/adev/src/content/tutorials/learn-angular/steps/15-forms/answer/src/app/app.component.ts_0_243
import {Component} from '@angular/core'; import {UserComponent} from './user.component'; @Component({ standalone: true, selector: 'app-root', template: ` <app-user /> `, imports: [UserComponent], }) export class AppComponent {}
{ "end_byte": 243, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/15-forms/answer/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/15-forms/answer/src/app/user.component.ts_0_524
import {Component} from '@angular/core'; import {FormsModule} from '@angular/forms'; @Component({ selector: 'app-user', template: ` <p>Username: {{ username }}</p> <p>{{ username }}'s favorite framework: {{ favoriteFramework }}</p> <label for="framework"> Favorite Framework: <input id="framework" type="text" [(ngModel)]="favoriteFramework" /> </label> `, standalone: true, imports: [FormsModule], }) export class UserComponent { favoriteFramework = ''; username = 'youngTech'; }
{ "end_byte": 524, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/15-forms/answer/src/app/user.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/15-forms/src/app/app.component.ts_0_243
import {Component} from '@angular/core'; import {UserComponent} from './user.component'; @Component({ standalone: true, selector: 'app-root', template: ` <app-user /> `, imports: [UserComponent], }) export class AppComponent {}
{ "end_byte": 243, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/15-forms/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/15-forms/src/app/user.component.ts_0_382
import {Component} from '@angular/core'; @Component({ selector: 'app-user', template: ` <p>Username: {{ username }}</p> <p>{{ username }}'s favorite framework: {{ favoriteFramework }}</p> <label for="framework">Favorite Framework:</label> `, standalone: true, imports: [], }) export class UserComponent { username = 'youngTech'; favoriteFramework = ''; }
{ "end_byte": 382, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/15-forms/src/app/user.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/21-constructor-based-di/README.md_0_1828
# Constructor-based dependency injection In previous activities you used the `inject()` function to make resources available, "providing" them to your components. The `inject()` function is one pattern and it is useful to know that there is another pattern for injecting resources called constructor-based dependency injection. You specify the resources as parameters to the `constructor` function of a component. Angular will make those resources available to your component. <br><br> In this activity, you will learn how to use constructor-based dependency injection. <hr> To inject a service or some other injectable resource into your component use the following syntax: <docs-code language="ts" highlight="[3]"> @Component({...}) class PetCarDashboardComponent { constructor(private petCareService: PetCareService) { ... } } </docs-code> There are a few things to notice here: - Use the `private` keyword - The `petCareService` becomes a property you can use in your class - The `PetCareService` class is the injected class Alright, now you give this a try: <docs-workflow> <docs-step title="Update the code to use constructor-based DI"> In `app.component.ts`, update the constructor code to match the code below: Tip: Remember, if you get stuck refer to the example on this activity page. ```ts constructor(private carService: CarService) { this.display = this.carService.getCars().join(' ⭐️ '); } ``` </docs-step> </docs-workflow> Congratulations on completing this activity. The example code works the same as with using the `inject` function. While these two approaches are largely the same, there are some small differences that are beyond the scope of this tutorial. <br> You can find out more information about dependency injection in the [Angular Documentation](guide/di).
{ "end_byte": 1828, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/21-constructor-based-di/README.md" }
angular/adev/src/content/tutorials/learn-angular/steps/21-constructor-based-di/answer/src/app/car.service.ts_0_277
import {Injectable} from '@angular/core'; @Injectable({ providedIn: 'root', }) export class CarService { cars = ['Sunflower GT', 'Flexus Sport', 'Sprout Mach One']; getCars(): string[] { return this.cars; } getCar(id: number) { return this.cars[id]; } }
{ "end_byte": 277, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/21-constructor-based-di/answer/src/app/car.service.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/21-constructor-based-di/answer/src/app/app.component.ts_0_363
import {Component} from '@angular/core'; import {CarService} from './car.service'; @Component({ selector: 'app-root', template: ` <p>Car Listing: {{ display }}</p> `, standalone: true, }) export class AppComponent { display = ''; constructor(private carService: CarService) { this.display = this.carService.getCars().join(' ⭐️ '); } }
{ "end_byte": 363, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/21-constructor-based-di/answer/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/21-constructor-based-di/src/app/car.service.ts_0_277
import {Injectable} from '@angular/core'; @Injectable({ providedIn: 'root', }) export class CarService { cars = ['Sunflower GT', 'Flexus Sport', 'Sprout Mach One']; getCars(): string[] { return this.cars; } getCar(id: number) { return this.cars[id]; } }
{ "end_byte": 277, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/21-constructor-based-di/src/app/car.service.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/21-constructor-based-di/src/app/app.component.ts_0_275
import {Component, inject} from '@angular/core'; import {CarService} from './car.service'; @Component({ selector: 'app-root', template: ` <p>Car Listing: {{ display }}</p> `, standalone: true, }) export class AppComponent { display = ''; constructor() {} }
{ "end_byte": 275, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/21-constructor-based-di/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/8-input/README.md_0_2139
# Component Communication with `@Input` Sometimes app development requires you to send data into a component. This data can be used to customize a component or perhaps send information from a parent component to a child component. Angular uses a concept called `Input`. This is similar to `props` in other frameworks. To create an `Input` property, use the `@Input` decorator. In this activity, you'll learn how to use the `@Input` decorator to send information to components. <hr> To create an `Input` property, add the `@Input` decorator to a property of a component class: <docs-code header="user.component.ts" language="ts"> class UserComponent { @Input() occupation = ''; } </docs-code> When you are ready to pass in a value through an `Input`, values can be set in templates using the attribute syntax. Here's an example: <docs-code header="app.component.ts" language="angular-ts" highlight="[3]"> @Component({ ... template: `<app-user occupation="Angular Developer"></app-user>` }) class AppComponent {} </docs-code> Make sure you bind the property `occupation` in your `UserComponent`. <docs-code header="user.component.ts" language="angular-ts"> @Component({ ... template: `<p>The user's occupation is {{occupation}}</p>` }) </docs-code> <docs-workflow> <docs-step title="Define an `@Input` property"> Update the code in `user.component.ts` to define an `Input` property on the `UserComponent` called `name`. For now, set the initial value to `empty string`. Be sure to update the template to interpolate the `name` property at the end of the sentence. </docs-step> <docs-step title="Pass a value to the `@Input` property"> Update the code in `app.component.ts` to send in the `name` property with a value of `"Simran"`. <br> When the code has been successfully updated, the app will display `The user's name is Simran`. </docs-step> </docs-workflow> While this is great, it is only one direction of the component communication. What if you want to send information and data to a parent component from a child component? Check out the next lesson to find out. P.S. you are doing great - keep going 🎉
{ "end_byte": 2139, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/8-input/README.md" }
angular/adev/src/content/tutorials/learn-angular/steps/8-input/answer/src/app/app.component.ts_0_257
import {Component} from '@angular/core'; import {UserComponent} from './user.component'; @Component({ selector: 'app-root', template: ` <app-user name="Simran" /> `, standalone: true, imports: [UserComponent], }) export class AppComponent {}
{ "end_byte": 257, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/8-input/answer/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/8-input/answer/src/app/user.component.ts_0_222
import {Component, Input} from '@angular/core'; @Component({ selector: 'app-user', template: ` <p>The user's name is {{ name }}</p> `, standalone: true, }) export class UserComponent { @Input() name = ''; }
{ "end_byte": 222, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/8-input/answer/src/app/user.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/8-input/src/app/app.component.ts_0_243
import {Component} from '@angular/core'; import {UserComponent} from './user.component'; @Component({ selector: 'app-root', template: ` <app-user /> `, standalone: true, imports: [UserComponent], }) export class AppComponent {}
{ "end_byte": 243, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/8-input/src/app/app.component.ts" }
angular/adev/src/content/tutorials/learn-angular/steps/8-input/src/app/user.component.ts_0_188
import {Component, Input} from '@angular/core'; @Component({ selector: 'app-user', template: ` <p>The user's name is</p> `, standalone: true, }) export class UserComponent {}
{ "end_byte": 188, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/learn-angular/steps/8-input/src/app/user.component.ts" }
angular/adev/src/content/tutorials/deferrable-views/BUILD.bazel_0_413
load("//adev/shared-docs:index.bzl", "generate_guides", "generate_tutorial") package(default_visibility = ["//adev:__subpackages__"]) generate_guides( name = "deferrable-views-guides", srcs = glob(["**/*.md"]), ) filegroup( name = "files", srcs = glob( ["**/*"], exclude = ["**/*.md"], ), ) generate_tutorial( name = "deferrable-views", tutorial_srcs = ":files", )
{ "end_byte": 413, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/deferrable-views/BUILD.bazel" }
angular/adev/src/content/tutorials/deferrable-views/intro/README.md_0_392
# Deferrable views tutorial This interactive tutorial consists of lessons that introduce the Angular deferrable views concepts. ## How to use this tutorial Each step represents a concept in Angular deferrable views. You can do one, or all of them. If you get stuck, click "Reveal answer" at the top. Alright, let's [get started](/tutorials/deferrable-views/1-what-are-deferrable-views).
{ "end_byte": 392, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/deferrable-views/intro/README.md" }
angular/adev/src/content/tutorials/deferrable-views/intro/src/app/app.component.ts_0_174
import {Component} from '@angular/core'; @Component({ selector: 'app-root', template: ` Welcome to Angular! `, standalone: true, }) export class AppComponent {}
{ "end_byte": 174, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/deferrable-views/intro/src/app/app.component.ts" }
angular/adev/src/content/tutorials/deferrable-views/common/idx/dev.nix_0_1086
# To learn more about how to use Nix to configure your environment # see: https://developers.google.com/idx/guides/customize-idx-env { pkgs, ... }: { # Which nixpkgs channel to use. channel = "stable-23.11"; # or "unstable" # Use https://search.nixos.org/packages to find packages packages = [ pkgs.nodejs_18 ]; # Sets environment variables in the workspace env = {}; idx = { # Search for the extensions you want on https://open-vsx.org/ and use "publisher.id" extensions = [ "angular.ng-template" ]; workspace = { # Runs when a workspace is first created with this \`dev.nix\` file onCreate = { npm-install = "npm install --no-audit --prefer-offline"; }; # To run something each time the environment is rebuilt, use the \`onStart\` hook }; # Enable previews and customize configuration previews = { enable = true; previews = { web = { command = ["npm" "run" "start" "--" "--port" "$PORT" "--host" "0.0.0.0"]; manager = "web"; }; }; }; }; }
{ "end_byte": 1086, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/deferrable-views/common/idx/dev.nix" }
angular/adev/src/content/tutorials/deferrable-views/steps/2-loading-error-placeholder/README.md_0_3797
# @loading, @error and @placeholder blocks Deferrable views let you define content to be shown in different loading states. <div class="docs-table docs-scroll-track-transparent"> <table> <tr> <td><code>@placeholder</code></td> <td> By default, defer blocks do not render any content before they are triggered. The <code>@placeholder</code> is an optional block that declares content to show before the deferred content loads. Angular replaces the placeholder with the deferred content after loading completes. While this block is optional, the Angular team recommends always including a placeholder. <a href="https://angular.dev/guide/defer#triggers" target="_blank"> Learn more in the full deferrable views documentation </a> </td> </tr> <tr> <td><code>@loading</code></td> <td> This optional block allows you to declare content to be shown during the loading of any deferred dependencies. </td> </tr> <tr> <td><code>@error</code></td> <td> This block allows you to declare content which is shown if deferred loading fails. </td> </tr> </table> </div> The contents of all the above sub-blocks are eagerly loaded. Additionally, some features require a `@placeholder` block. In this activity, you'll learn how to use the `@loading`, `@error` and `@placeholder` blocks to manage the states of deferrable views. <hr> <docs-workflow> <docs-step title="Add `@placeholder` block"> In your `app.component.ts`, add a `@placeholder` block to the `@defer` block. <docs-code language="angular-html" highlight="[3,4,5]"> @defer { <article-comments /> } @placeholder { <p>Placeholder for comments</p> } </docs-code> </docs-step> <docs-step title="Configure the `@placeholder` block"> The `@placeholder` block accepts an optional parameter to specify the `minimum` amount of time that this placeholder should be shown. This `minimum` parameter is specified in time increments of milliseconds (ms) or seconds (s). This parameter exists to prevent fast flickering of placeholder content in the case that the deferred dependencies are fetched quickly. <docs-code language="angular-html" highlight="[3,4,5]"> @defer { <article-comments /> } @placeholder (minimum 1s) { <p>Placeholder for comments</p> } </docs-code> </docs-step> <docs-step title="Add `@loading` block"> Next add a `@loading` block to the component template. The `@loading` block accepts two optional parameters: * `minimum`: the amount of time that this block should be shown * `after`: the amount of time to wait after loading begins before showing the loading template Both parameters are specified in time increments of milliseconds (ms) or seconds (s). Update `app.component.ts` to include a `@loading` block with a minimum parameter of `1s` as well as an after parameter with the value 500ms to the @loading block. <docs-code language="angular-html" highlight="[5,6,7]"> @defer { <article-comments /> } @placeholder (minimum 1s) { <p>Placeholder for comments</p> } @loading (minimum 1s; after 500ms) { <p>Loading comments...</p> } </docs-code> Note: this example uses two parameters, separated by the ; character. </docs-step> <docs-step title="Add `@error` block"> Finally, add an `@error` block to the `@defer` block. <docs-code language="angular-html" highlight="[7,8,9]"> @defer { <article-comments /> } @placeholder (minimum 1s) { <p>Placeholder for comments</p> } @loading (minimum 1s; after 500ms) { <p>Loading comments...</p> } @error { <p>Failed to load comments</p> } </docs-code> </docs-step> </docs-workflow> Congratulations! At this point, you have a good understanding about deferrable views. Keep up the great work and let's learn about triggers next.
{ "end_byte": 3797, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/deferrable-views/steps/2-loading-error-placeholder/README.md" }
angular/adev/src/content/tutorials/deferrable-views/steps/2-loading-error-placeholder/answer/src/main.ts_0_241
import {bootstrapApplication} from '@angular/platform-browser'; import {AppComponent} from './app/app.component'; import {appConfig} from './app/app.config'; bootstrapApplication(AppComponent, appConfig).catch((err) => console.error(err));
{ "end_byte": 241, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/deferrable-views/steps/2-loading-error-placeholder/answer/src/main.ts" }
angular/adev/src/content/tutorials/deferrable-views/steps/2-loading-error-placeholder/answer/src/app/app.component.ts_0_880
import {Component} from '@angular/core'; import {ArticleCommentsComponent} from './article-comments.component'; @Component({ selector: 'app-root', template: ` <div> <h1>How I feel about Angular</h1> <article> <p> Angular is my favorite framework, and this is why. Angular has the coolest deferrable view feature that makes defer loading content the easiest and most ergonomic it could possibly be. </p> </article> @defer { <article-comments /> } @placeholder (minimum 1s) { <p>Placeholder for comments</p> } @loading (minimum 1s; after 500ms) { <p>Loading comments...</p> } @error { <p>Failed to load comments</p> } </div> `, standalone: true, imports: [ArticleCommentsComponent], }) export class AppComponent {}
{ "end_byte": 880, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/deferrable-views/steps/2-loading-error-placeholder/answer/src/app/app.component.ts" }
angular/adev/src/content/tutorials/deferrable-views/steps/2-loading-error-placeholder/answer/src/app/app.config.ts_0_116
import {ApplicationConfig} from '@angular/core'; export const appConfig: ApplicationConfig = { providers: [], };
{ "end_byte": 116, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/deferrable-views/steps/2-loading-error-placeholder/answer/src/app/app.config.ts" }
angular/adev/src/content/tutorials/deferrable-views/steps/2-loading-error-placeholder/answer/src/app/article-comments.component.ts_0_571
import {Component} from '@angular/core'; @Component({ selector: 'article-comments', template: ` <h2>Comments</h2> <p class="comment"> Building for the web is fantastic! </p> <p class="comment"> The new template syntax is great </p> <p class="comment"> I agree with the other comments! </p> `, styles: [ ` .comment { padding: 15px; margin-left: 30px; background-color: paleturquoise; border-radius: 20px; } `, ], standalone: true, }) export class ArticleCommentsComponent {}
{ "end_byte": 571, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/deferrable-views/steps/2-loading-error-placeholder/answer/src/app/article-comments.component.ts" }
angular/adev/src/content/tutorials/deferrable-views/steps/2-loading-error-placeholder/src/main.ts_0_241
import {bootstrapApplication} from '@angular/platform-browser'; import {AppComponent} from './app/app.component'; import {appConfig} from './app/app.config'; bootstrapApplication(AppComponent, appConfig).catch((err) => console.error(err));
{ "end_byte": 241, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/deferrable-views/steps/2-loading-error-placeholder/src/main.ts" }
angular/adev/src/content/tutorials/deferrable-views/steps/2-loading-error-placeholder/src/app/app.component.ts_0_668
import {Component} from '@angular/core'; import {ArticleCommentsComponent} from './article-comments.component'; @Component({ selector: 'app-root', template: ` <div> <h1>How I feel about Angular</h1> <article> <p> Angular is my favorite framework, and this is why. Angular has the coolest deferrable view feature that makes defer loading content the easiest and most ergonomic it could possibly be. </p> </article> @defer { <article-comments /> } </div> `, standalone: true, imports: [ArticleCommentsComponent], }) export class AppComponent {}
{ "end_byte": 668, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/deferrable-views/steps/2-loading-error-placeholder/src/app/app.component.ts" }
angular/adev/src/content/tutorials/deferrable-views/steps/2-loading-error-placeholder/src/app/app.config.ts_0_116
import {ApplicationConfig} from '@angular/core'; export const appConfig: ApplicationConfig = { providers: [], };
{ "end_byte": 116, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/deferrable-views/steps/2-loading-error-placeholder/src/app/app.config.ts" }
angular/adev/src/content/tutorials/deferrable-views/steps/2-loading-error-placeholder/src/app/article-comments.component.ts_0_571
import {Component} from '@angular/core'; @Component({ selector: 'article-comments', template: ` <h2>Comments</h2> <p class="comment"> Building for the web is fantastic! </p> <p class="comment"> The new template syntax is great </p> <p class="comment"> I agree with the other comments! </p> `, styles: [ ` .comment { padding: 15px; margin-left: 30px; background-color: paleturquoise; border-radius: 20px; } `, ], standalone: true, }) export class ArticleCommentsComponent {}
{ "end_byte": 571, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/deferrable-views/steps/2-loading-error-placeholder/src/app/article-comments.component.ts" }
angular/adev/src/content/tutorials/deferrable-views/steps/1-what-are-deferrable-views/README.md_0_1585
# What are deferrable views? A fully rendered Angular page may contain many different components, directives, and pipes. While certain parts of the page should be shown to the user immediately, there may be portions that can wait to display until later. Angular's *deferrable views*, using the `@defer` syntax, can help you speed up your application by telling Angular to wait to download the JavaScript for the parts of the page that don't need to be shown right away. In this activity, you'll learn how to use deferrable views to defer load a section of your component template. <hr> <docs-workflow> <docs-step title="Add a `@defer` block to a section of a template"> In your `app.component.ts`, wrap the `article-comments` component with a `@defer` block to defer load it. <docs-code language="angular-html"> @defer { <article-comments /> } </docs-code> By default, `@defer` loads the `article-comments` component when the browser is idle. In your browser's developer console, you can see that the `article-comments-component` lazy chunk file is loaded separately (The specific file names may change from run to run): <docs-code language="markdown"> Initial chunk files | Names | Raw size chunk-NNSQHFIE.js | - | 769.00 kB | main.js | main | 229.25 kB | Lazy chunk files | Names | Raw size chunk-T5UYXUSI.js | article-comments-component | 1.84 kB | </docs-code> </docs-step> </docs-workflow> Great work! You’ve learned the basics of deferrable views.
{ "end_byte": 1585, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/deferrable-views/steps/1-what-are-deferrable-views/README.md" }
angular/adev/src/content/tutorials/deferrable-views/steps/1-what-are-deferrable-views/answer/src/main.ts_0_241
import {bootstrapApplication} from '@angular/platform-browser'; import {AppComponent} from './app/app.component'; import {appConfig} from './app/app.config'; bootstrapApplication(AppComponent, appConfig).catch((err) => console.error(err));
{ "end_byte": 241, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/deferrable-views/steps/1-what-are-deferrable-views/answer/src/main.ts" }
angular/adev/src/content/tutorials/deferrable-views/steps/1-what-are-deferrable-views/answer/src/app/app.component.ts_0_668
import {Component} from '@angular/core'; import {ArticleCommentsComponent} from './article-comments.component'; @Component({ selector: 'app-root', template: ` <div> <h1>How I feel about Angular</h1> <article> <p> Angular is my favorite framework, and this is why. Angular has the coolest deferrable view feature that makes defer loading content the easiest and most ergonomic it could possibly be. </p> </article> @defer { <article-comments /> } </div> `, standalone: true, imports: [ArticleCommentsComponent], }) export class AppComponent {}
{ "end_byte": 668, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/deferrable-views/steps/1-what-are-deferrable-views/answer/src/app/app.component.ts" }
angular/adev/src/content/tutorials/deferrable-views/steps/1-what-are-deferrable-views/answer/src/app/app.config.ts_0_116
import {ApplicationConfig} from '@angular/core'; export const appConfig: ApplicationConfig = { providers: [], };
{ "end_byte": 116, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/deferrable-views/steps/1-what-are-deferrable-views/answer/src/app/app.config.ts" }
angular/adev/src/content/tutorials/deferrable-views/steps/1-what-are-deferrable-views/answer/src/app/article-comments.component.ts_0_571
import {Component} from '@angular/core'; @Component({ selector: 'article-comments', template: ` <h2>Comments</h2> <p class="comment"> Building for the web is fantastic! </p> <p class="comment"> The new template syntax is great </p> <p class="comment"> I agree with the other comments! </p> `, styles: [ ` .comment { padding: 15px; margin-left: 30px; background-color: paleturquoise; border-radius: 20px; } `, ], standalone: true, }) export class ArticleCommentsComponent {}
{ "end_byte": 571, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/deferrable-views/steps/1-what-are-deferrable-views/answer/src/app/article-comments.component.ts" }
angular/adev/src/content/tutorials/deferrable-views/steps/1-what-are-deferrable-views/src/main.ts_0_241
import {bootstrapApplication} from '@angular/platform-browser'; import {AppComponent} from './app/app.component'; import {appConfig} from './app/app.config'; bootstrapApplication(AppComponent, appConfig).catch((err) => console.error(err));
{ "end_byte": 241, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/deferrable-views/steps/1-what-are-deferrable-views/src/main.ts" }
angular/adev/src/content/tutorials/deferrable-views/steps/1-what-are-deferrable-views/src/app/app.component.ts_0_643
import {Component} from '@angular/core'; import {ArticleCommentsComponent} from './article-comments.component'; @Component({ selector: 'app-root', template: ` <div> <h1>How I feel about Angular</h1> <article> <p> Angular is my favorite framework, and this is why. Angular has the coolest deferrable view feature that makes defer loading content the easiest and most ergonomic it could possibly be. </p> </article> <article-comments /> </div> `, standalone: true, imports: [ArticleCommentsComponent], }) export class AppComponent {}
{ "end_byte": 643, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/deferrable-views/steps/1-what-are-deferrable-views/src/app/app.component.ts" }
angular/adev/src/content/tutorials/deferrable-views/steps/1-what-are-deferrable-views/src/app/app.config.ts_0_116
import {ApplicationConfig} from '@angular/core'; export const appConfig: ApplicationConfig = { providers: [], };
{ "end_byte": 116, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/deferrable-views/steps/1-what-are-deferrable-views/src/app/app.config.ts" }
angular/adev/src/content/tutorials/deferrable-views/steps/1-what-are-deferrable-views/src/app/article-comments.component.ts_0_571
import {Component} from '@angular/core'; @Component({ selector: 'article-comments', template: ` <h2>Comments</h2> <p class="comment"> Building for the web is fantastic! </p> <p class="comment"> The new template syntax is great </p> <p class="comment"> I agree with the other comments! </p> `, styles: [ ` .comment { padding: 15px; margin-left: 30px; background-color: paleturquoise; border-radius: 20px; } `, ], standalone: true, }) export class ArticleCommentsComponent {}
{ "end_byte": 571, "start_byte": 0, "url": "https://github.com/angular/angular/blob/main/adev/src/content/tutorials/deferrable-views/steps/1-what-are-deferrable-views/src/app/article-comments.component.ts" }