官术网_书友最值得收藏!

Creating a task list

Now that we have our main application component set up, we can go on and start fleshing out our task management application. The second component that we're going to create will be responsible for listing tasks. Following the concept of composition, we'll create a task list component as a subcomponent of our main application component.

Let's create a new component for our task list by using the Angular CLI generator functionality. We already want to structure our application by area, where we put all task-relevant components into a tasks subfolder:

ng generate component --spec false -ve none tasks/task-list

Using the --spec false option while generating our component, we can skip creating test specifications. Since we're going to cover testing in a later chapter, we're skipping this process for the moment. Also, by using the -ve none parameter, we can tell Angular to create the component using ViewEncapsulation.None as a default encapsulation setting.

If you're using the Angular CLI tool to generate components, they will be automatically added to your main module. This is really handy and offloads a lot of boilerplate work on your side. If you're creating a component manually, you should never forget to include the newly created component within your NgModule declarations.

Let's open the generated file src/app/tasks/task-list.ts and do some modifications to it:

import {Component, ViewEncapsulation} from '@angular/core';

@Component({ 
  selector: 'mac-task-list',
templateUrl: './task-list.component.html',
encapsulation: ViewEncapsulation.None
})
export class TaskListComponent {
tasks = [
{id: 1, title: 'Task 1', done: false},
{id: 2, title: 'Task 2', done: true}
];
}

We've created a very simple task list component that has a list of tasks stored internally. This component will be attached to HTML elements that match the CSS element selector mac-task-list.

Now, let's create a view template for this component to display the tasks. As you can see from the templateUrl property within the component metadata, we are looking for a file called task-list.component.html.

Let's change the content of this file to match the following excerpt:

<div *ngFor="let task of tasks"> 
  <input type="checkbox" [checked]="task.done"> 
  <div>{{task.title}}</div> 
</div> 

We use the NgFor directive to repeat the outermost DIV element for as many tasks as we have on the task list of our component. The NgFor directive in Angular will create a template element from its underlying content and instantiate as many elements from the template as the expression evaluates to. We currently have two tasks in our task list component, so this will create two instances of our template.

All that's left to do in order to make our task list work is include the task list component within the main application component. We can go ahead and modify our src/app/app.component.html file and change its content to match the following:

<mac-task-list></mac-task-list>

This was the last change we needed to make in order to make our task list component work. To view your changes, you can start the serving mode of Angular CLI, if you don't have it running already.

主站蜘蛛池模板: 衢州市| 芦溪县| 咸丰县| 镇坪县| 化州市| 利津县| 阿拉善盟| 宁国市| 柳林县| 恭城| 鄂托克前旗| 凤阳县| 永寿县| 高唐县| 锡林浩特市| 新巴尔虎左旗| 英超| 锦屏县| 喀什市| 临清市| 孟连| 沂水县| 西城区| 云浮市| 天津市| 景洪市| 江西省| 民乐县| 靖安县| 恭城| 普兰店市| 甘洛县| 黄陵县| 双峰县| 新河县| 安平县| 正定县| 肥乡县| 江孜县| 剑川县| 琼海市|