介绍Angular 2的书,第1章,示例5
Unraveling Angular 2 book, Chapter 1, Example 5
页面显示了一个潜水列表,它有一个"添加新潜水","清除潜水"和一个搜索框,当你输入时,它会过滤显示的列表。
这是模板:
<div class="container-fluid">
<h1>My Latest Dives (Angular/TypeScript)</h1>
<div class="row">
<div class="col-sm-5">
<button class="btn btn-primary btn-lg"
[disabled]="!enableAdd()"
(click)="addDive()">
Add new dive
</button>
<button class="btn btn-danger btn-lg"
(click)="clearDives()">
Clear dives
</button>
</div>
<div class="col-sm-4 col-sm-offset-3">
<input #searchBox class="form-control input-lg"
placeholder="Search"
(keyup)="0" />
</div>
</div>
<div class="row">
<div class="col-sm-4"
*ngFor="let dive of dives | contentFilter:searchBox.value">
<h3>{{dive.site}}</h3>
<h4>{{dive.location}}</h4>
<h2>{{dive.depth}} feet | {{dive.time}} min</h2>
</div>
</div>
</div>
组件代码:
import {Component} from '@angular/core';
@Component({
selector: 'divelog',
templateUrl: 'app/dive-log.template.html'
})
export class DiveLogComponent {
public dives = [];
private _index = 0;
private _stockDives = [
{
site: 'Abu Gotta Ramada',
location: 'Hurghada, Egypt',
depth: 72,
time: 54
},
{
site: 'Ponte Mahoon',
location: 'Maehbourg, Mauritius',
depth: 54,
time: 38
},
{
site: 'Molnar Cave',
location: 'Budapest, Hungary',
depth: 98,
time: 62
}];
public enableAdd() {
return this._index < this._stockDives.length;
}
public addDive() {
if (this.enableAdd()) {
this.dives.push(this._stockDives[this._index++]);
}
}
public clearDives() {
this.dives = [];
this._index = 0;
}
}
这是过滤器代码:
import {Pipe, PipeTransform} from '@angular/core';
@Pipe({name: 'contentFilter'})
export class ContentFilterPipe implements PipeTransform {
transform(value: any[], searchFor: string) : any[] {
if (!searchFor) return value;
searchFor = searchFor.toLowerCase();
return value.filter(dive =>
dive.site.toLowerCase().indexOf(searchFor) >= 0 ||
dive.location.toLowerCase().indexOf(searchFor) >= 0 ||
dive.depth.toString().indexOf(searchFor) >= 0 ||
dive.time.toString().indexOf(searchFor) >= 0);
}
}
当我在搜索框中输入时,过滤器被调用,列表被重新呈现,但当我点击"添加"按钮时却没有。如果我在搜索框中有一些东西,"添加"按钮不会导致潜水列表的变化,即使搜索框的内容允许显示新项目。如何更改代码,以便单击"添加"按钮将导致重新呈现显示的潜水列表?
你有一个纯管道所以
它的方法
transform
只会在它检测到一个纯更改为输入值。
适合你的情况
*ngFor="let dive of dives | contentFilter:searchBox.value"
输入值将为dives
和searchBox.value
根据angular2管道指南:
纯更改是对原始输入值的更改(字符串,数字,布尔值,符号)或更改的对象引用(日期,数组、函数、对象).
- 当添加
dive
时,数组引用(dives)不会改变-因此transform
方法不会执行。 - 当某些东西被输入到过滤器输入,
searchBox.value
确实改变-因此执行transform
。
所以一个可能的解决方案是每次添加div时总是有一个新的引用数组:
替换:
this.dives.push(this._stockDives[this._index++]);
:
this.dives = this.dives.concat(this._stockDives[this._index++]);
或:
this.dives = [...this.dives, this._stockDives[this._index++]];
第二种方法是使用非纯管道:
@Pipe({name: 'contentFilter', pure: false })
相关文章:
- PHP AJAX图片上传示例不上传
- API密钥使用和检查示例
- 三星智能电视应用程序;Brightcove示例应用程序远程控制问题
- 骨干模型默认值-todos.js示例中不必要的代码
- d3js文本传输-示例代码不起作用
- Eloquent JavaScript递归示例如何终止为返回1,但仍然输出指数值
- JQuery示例不起作用-“;对象没有't支持属性或方法'按钮'&”;
- 来自MDN的循环示例的JavaScript不起作用
- 如何使相机跟随地形's在此示例中的高度:http://threejs.org/examples/#webgl_t
- 谷歌应用程序脚本示例-如何更改单选按钮的列表框相关值
- AngularJS控制器不工作,为什么?(简单的控制器示例)
- d3绘制动画折线图的基本示例
- JavaScript原型示例
- 如何将此示例函数调整为Backbone
- 无法在我的示例plunker中使用继承/隔离的范围概念
- onbeforeunload事件在我的代码中没有触发,但其他示例可以工作
- 承诺-如何正确地将错误附加到此示例
- JSON:loop Invoice Items:如果InvoiceNo未退出则添加,如果退出则更新值,示例包括在内
- 介绍Angular 2的书,第1章,示例5
- XMPP - 示例第 3 章“使用 JavaScript 和 jQuery 进行专业 XMPP 编程”不起作用