带组件变量的角 2 管/过滤器
Angular 2 pipe/filter with component variable
我有显示本周和下周事件的应用程序。周通过两个按钮切换。我需要过滤器/管道,它只会显示当前活动周的事件。我在 Angular 1 中有这个过滤器,一切正常,但在 2 中我无法让这个过滤器工作。请参阅代码:
@Page({
templateUrl: 'build/pages/lunches/lunches.html',
providers: [LunchService, HTTP_PROVIDERS],
})
export class Lunches {
weekDuration:number = 24 * 60 * 60 * 7 * 1000;
timestampFix:number = 24 * 60 * 60 * 4 * 1000;
currentWeekNumber:number = parseInt(new Date().getTime() / this.weekDuration);
nextWeekNumber:number = this.currentWeekNumber + 1;
activeWeekNumber:string = '2408';
lunches:Array;
activeLunches:Array;
noData:boolean = true;
constructor(private lunchService:LunchService) {
}
ngOnInit() {
this.loadLunches();
}
loadLunches() {
this.lunchService.getLunchList().subscribe(
data => {
var lunches = data;
lunches.forEach((item, index) => {
lunches[index].date = new Date(item.date);
});
this.lunches = lunches;
this.weekFilter();
}
);
}
weekFilter() {
var activeLunches = [];
this.lunches.forEach((item, index) => {
var lunchDate = new Date(item.date).getTime() - this.timestampFix;
console.log(this.currentWeekNumber, ' = ', parseInt(lunchDate / this.weekDuration));
if (this.activeWeekNumber == parseInt(lunchDate / this.weekDuration)) {
this.noData = false;
activeLunches.push(item);
}
});
this.activeLunches = activeLunches;
}
}
模板:
<div padding>
<ion-segment [(ngModel)]="activeWeekNumber" (click)="weekFilter()">
<ion-segment-button value="{{currentWeekNumber}}" selected>
This week
</ion-segment-button>
<ion-segment-button value="{{nextWeekNumber}}">
Next
</ion-segment-button>
</ion-segment>
</div>
<ion-card *ngIf="noData">
<ion-card-content>
No lunches for this week.
</ion-card-content>
</ion-card>
<ion-card *ngFor="#lunch of activeLunches">
<ion-card-header>
<h2>{{lunch.date | date: "E d. M."}}</h2>
</ion-card-header>
</ion-card>
这是我的实际代码,效果不佳,我认为它的解决方案很糟糕。我尝试将其编写为管道,但我无法访问和更改变量 noData。
编辑:根据评论,我决定使用此解决方案,但是每次活动周变量更改时如何触发周过滤器函数存在问题。有什么想法吗?
在以下情况下收到通知
activeWeekNumber:string = '2408';
变化有不同的方式。
- 您可以将字段更改为吸气手/二传手
_activeWeekNumber:string = '2408';
get activeWeekNumber():string {
return this._activeWeekNumber;
}
set activeWeekNumber(value:string) {
this._activeWeekNumber = value;
}
- 或使用 ngModel 中的事件
<ion-segment [(ngModel)]="activeWeekNumber" (ngModelChange)="doSomething($event) (click)="weekFilter()"
相关文章:
- React重新渲染但未显示正确的组件
- 不能从angular2中的子组件指定父组件中的数组
- 拨打'父亲'函数形式a'儿童'ReactJS中的组件
- React组件等待所需道具进行渲染
- 如何创建带有插槽的vue js组件预加载程序
- KnockoutJS-组件-多个实例
- 如何更改reactjs中外部/独立组件的状态或属性
- 为什么我的d3.jsselectAll+过滤器没有过滤
- Angularjs-utils高亮过滤器在搜索时破坏应用程序
- 如何在react js中将值从一个组件发送到另一个组件
- React Native通过Navigator将用户输入数据传递到选项卡栏IOS中的组件
- React组件-设置页面标题
- 我的shareService在angular 2中发送值工作正常,但当我渲染我的另一个组件时,会获得默认值
- 如何在salesforce aura闪电组件中使用jQuery.val()
- Angular-表的多列过滤器
- 为react组件传递道具的最佳方式
- React 组件中的 AngularJS 过滤器
- 带组件变量的角 2 管/过滤器
- 如何在 Vuejs 组件中应用过滤器
- 反应过滤器组件的状态生命周期