Angular 4入门教程系列:4:Tour Of Heroes之事件处理

这里写图片描述
上篇文章我们学习了对象的插值表达式和双向数据绑定,这篇我们将学习一下ngFor与ngIf的写法以及事件的处理方式。

学习目标

这篇文章将实现如下目标:

  • 显示英雄对象的列表
  • 当点击对象的时候,显示该对象的详细信息

这里写图片描述

学习时长

大概5-10分钟

对象数组定义

在组件文件中定义一个对象数组,用以存放HERO的列表

const HEROES: Hero[] = [
  { id: 11, name: 'Mr. Nice' },
  { id: 12, name: 'Narco' },
  { id: 13, name: 'Bombasto' },
  { id: 14, name: 'Celeritas' },
  { id: 15, name: 'Magneta' },
  { id: 16, name: 'RubberMan' },
  { id: 17, name: 'Dynama' },
  { id: 18, name: 'Dr IQ' },
  { id: 19, name: 'Magma' },
  { id: 20, name: 'Tornado' }
];

修改HTML模板进行显示

/workspace/HelloAngular/src/app # cat app.component.html
  <h1>{{title}}</h1>
  <h2>My Heroes</h2>
  <ul class="heroes">
    <li *ngFor="let hero of heroes">
       <span class="badge">{{hero.id}}</span> {{hero.name}}
    </li> 
  </ul>
/workspace/HelloAngular/src/app # 

这里用到了ngFor语句,这是一条循环语句,它会从组件中取得列表信息heroes,而heroes在app.component.ts中被定义,正是上面设定的HEROES的常量数组,详细参看如下:

/workspace/HelloAngular/src/app # cat app.component.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'Tour of Heroes';
  heroes = HEROES;
}
export class Hero {
  id: number;
  name: string;
}
const HEROES: Hero[] = [
  { id: 11, name: 'Mr. Nice' },
  { id: 12, name: 'Narco' },
  { id: 13, name: 'Bombasto' },
  { id: 14, name: 'Celeritas' },
  { id: 15, name: 'Magneta' },
  { id: 16, name: 'RubberMan' },
  { id: 17, name: 'Dynama' },
  { id: 18, name: 'Dr IQ' },
  { id: 19, name: 'Magma' },
  { id: 20, name: 'Tornado' }
];
/workspace/HelloAngular/src/app # 

结果页面

结果页面显示如下
这里写图片描述

修改样式

因为此处还是用的上个例子的样式,所以可以看到h1还是跟上次一样,我们修改一下样式文件的内容:

/workspace/HelloAngular/src/app # cat app.component.css
  .selected {
    background-color: #CFD8DC !important;
    color: white;
  }
  .heroes {
    margin: 0 0 2em 0;
    list-style-type: none;
    padding: 0;
    width: 15em;
  }
  .heroes li {
    cursor: pointer;
    position: relative;
    left: 0;
    background-color: #EEE;
    margin: .5em;
    padding: .3em 0;
    height: 1.6em;
    border-radius: 4px;
  }
  .heroes li.selected:hover {
    background-color: #BBD8DC !important;
    color: white;
  }
  .heroes li:hover {
    color: #607D8B;
    background-color: #DDD;
    left: .1em;
  }
  .heroes .text {
    position: relative;
    top: -3px;
  }
  .heroes .badge {
    display: inline-block;
    font-size: small;
    color: white;
    padding: 0.8em 0.7em 0 0.7em;
    background-color: #607D8B;
    line-height: 1em;
    position: relative;
    left: -1px;
    top: -4px;
    height: 1.8em;
    margin-right: .8em;
    border-radius: 4px 0 0 4px;
  }
/workspace/HelloAngular/src/app # 

确认样式修正后的显示
这里写图片描述

添加Click事件对应

为了能够实现点击某个li能够返回具体的信息,我们需要做如下几件事情

  • 在li标签中添加Click事件的对应,并将ngFor的参数传递进去
  • 在组件中定义一个用于保存选择信息的对象
  • 在组件中进行Click事件的对应并保存传过来的对象值
  • 将此值进行ngModel绑定进行显示

HTML页面代码

使用ngIf的原因是为了判空,在为选中时空值会使得出现undefined的问题,代码如下

/workspace/HelloAngular/src/app # cat app.component.html
  <h1>{{title}}</h1>
  <h2>My Heroes</h2>
  <ul class="heroes">
    <li *ngFor="let hero of heroes" (click)="onSelect(hero)">
       <span class="badge">{{hero.id}}</span> {{hero.name}}
    </li> 
  </ul>

  <div *ngIf="selectedHero">
    <h2>{{selectedHero.name}} details!</h2>
    <div><label>id: </label>{{selectedHero.id}}</div>
    <div>
      <label>name: </label>
      <input [(ngModel)]="selectedHero.name" placeholder="name"/>
    </div>
  </div>
/workspace/HelloAngular/src/app #

组件侧代码

/workspace/HelloAngular/src/app # cat app.component.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'Tour of Heroes';
  heroes = HEROES;
  selectedHero: Hero;

  onSelect(hero: Hero): void {
    this.selectedHero = hero;
  }
}
export class Hero {
  id: number;
  name: string;
}
const HEROES: Hero[] = [
  { id: 11, name: 'Mr. Nice' },
  { id: 12, name: 'Narco' },
  { id: 13, name: 'Bombasto' },
  { id: 14, name: 'Celeritas' },
  { id: 15, name: 'Magneta' },
  { id: 16, name: 'RubberMan' },
  { id: 17, name: 'Dynama' },
  { id: 18, name: 'Dr IQ' },
  { id: 19, name: 'Magma' },
  { id: 20, name: 'Tornado' }
];
/workspace/HelloAngular/src/app # 

结果的显示

选择第一个HERO,结果如下
这里写图片描述

选中的项目的样式设定

目前还有一个问题就是页面的选中的id为11的HERO和其他的对象没有区别,我们需要给li添加一个样式,具体写法为 [class.selected]=”hero === selectedHero”,表示如果此项目和选中的项目值相同的话就使用selected样式,加上h1的样式,具体代码如下:

/workspace/HelloAngular/src/app # cat app.component.css
h1 {
  color: #369;
  font-family: Arial, Helvetica, sans-serif;
  font-size: 250%;
}
  .selected {
    background-color: #CFD8DC !important;
    color: white;
  }
  .heroes {
    margin: 0 0 2em 0;
    list-style-type: none;
    padding: 0;
    width: 15em;
  }
  .heroes li {
    cursor: pointer;
    position: relative;
    left: 0;
    background-color: #EEE;
    margin: .5em;
    padding: .3em 0;
    height: 1.6em;
    border-radius: 4px;
  }
  .heroes li.selected:hover {
    background-color: #BBD8DC !important;
    color: white;
  }
  .heroes li:hover {
    color: #607D8B;
    background-color: #DDD;
    left: .1em;
  }
  .heroes .text {
    position: relative;
    top: -3px;
  }
  .heroes .badge {
    display: inline-block;
    font-size: small;
    color: white;
    padding: 0.8em 0.7em 0 0.7em;
    background-color: #607D8B;
    line-height: 1em;
    position: relative;
    left: -1px;
    top: -4px;
    height: 1.8em;
    margin-right: .8em;
    border-radius: 4px 0 0 4px;
  }
/workspace/HelloAngular/src/app # 

HTML模板代码如下:

/workspace/HelloAngular/src/app # cat app.component.html
  <h1>{{title}}</h1>
  <h2>My Heroes</h2>
  <ul class="heroes">
    <li *ngFor="let hero of heroes"  [class.selected]="hero === selectedHero" (click)="onSelect(hero)">
       <span class="badge">{{hero.id}}</span> {{hero.name}}
    </li> 
  </ul>

  <div *ngIf="selectedHero">
    <h2>{{selectedHero.name}} details!</h2>
    <div><label>id: </label>{{selectedHero.id}}</div>
    <div>
      <label>name: </label>
      <input [(ngModel)]="selectedHero.name" placeholder="name"/>
    </div>
  </div>
/workspace/HelloAngular/src/app #

结果显示如下
这里写图片描述

总结

这篇文章我们学习了如何进行事件的处理,以及ngFor和ngIf的使用方法,在下一章将会关注Angular组件的复用和组件之间的交互。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你是初学者,以下是一个简单的 Angular 入门教程,帮助你开始学习 Angular: 1. 安装 Angular CLI:首先,你需要安装 Node.js 和 npm(Node 包管理器)。然后,使用 npm 安装 Angular CLI。在命令行中运行以下命令: ``` npm install -g @angular/cli ``` 2. 创建新的 Angular 项目:使用 Angular CLI 创建一个新的 Angular 项目。在命令行中运行以下命令: ``` ng new my-app ``` 这将创建一个名为 "my-app" 的新项目,并自动安装所需的依赖项。 3. 运行应用程序:进入项目目录,然后使用以下命令启动开发服务器: ``` cd my-app ng serve ``` 这将启动一个开发服务器,并在默认端口上运行你的应用程序(通常是 http://localhost:4200)。 4. 编辑应用程序:打开你喜欢的代码编辑器,并导航到 "src/app" 目录。在这里,你将找到应用程序的根组件 "app.component.ts"。你可以编辑这个文件并开始构建你的应用程序。 5. 添加新组件:使用 Angular CLI 生成一个新的组件。在命令行中运行以下命令: ``` ng generate component my-component ``` 这将生成一个名为 "my-component" 的新组件,并自动更新应用程序中的文件和配置。 6. 在应用程序中使用组件:在根组件的模板中使用新生成的组件,或者在其他组件中使用它。 以上是一个简单的 Angular 入门教程。当你熟悉了这些基础知识后,你可以继续学习 Angular 的更多高级主题,如路由、表单处理、服务等。记得查阅官方文档和其他教程资源以获得更多深入的学习。祝你学习愉快!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值