简体   繁体   English

管道的两种方式绑定未在 angular2 中绑定

[英]Two way binding with pipe not binding in angular2

In my component html displaying the data based on the current year using the filter, it works fine on load.在我的组件 html 中,使用过滤器显示基于当前年份的数据,它在加载时工作正常。 If i do push or pop on the variable updating the html also, but i use filter (pipe) not updating the html view.如果我也推送或弹出更新 html 的变量,但我使用过滤器(管道)而不更新 html 视图。 onSubmit pushing the new data value to the projects (appropriate), it's reflecting (updating the html) If didn't use filter. onSubmit 将新数据值推送到项目(适当),它反映(更新 html)如果没有使用过滤器。

data.component.ts数据组件.ts

 constructor() {
        this.projects = (from service getting the data);
        var newDate = new Date();
        newDate.setDate(1);
        this.dateValue = newDate;
    }
onSubmit(data) {
   this.projects.push(data);
}

data.component.html数据组件.html

<ul class="list-group">
      <li *ngFor="let event of projects | yearFilter: dateValue" class="list-group-item">
        <span class="event_release">{{event.startDate | date: 'dd/MM/yyyy'}} </span><br />
        <strong>{{event.title}}</strong>
      </li>
    </ul>
    <ul class="list-group">
      <li *ngFor="let data of projects" class="list-group-item">
        <span class="event_release">{{data.startDate | date: 'dd/MM/yyyy'}} </span><br />
        <strong>{{data.title}}</strong>
      </li>
    </ul>

year-filter.pipe.ts年-filter.pipe.ts

export class YearFilterPipe implements PipeTransform {
  transform(items: any[], args: any): any {
    return items.filter(item => {
        var startDate = new Date(item.startDate);
        var endDate = new Date(item.endDate);
        var realEndDate = new Date(item.realEndDate);
        return ((startDate.getFullYear()===args.getFullYear());
    });
  }
}

In your Pipe use this:在你的管道中使用这个:

@Pipe({ name: 'yearFilter', pure: false }) 

Your need to set properties pure to false.您需要将纯属性设置为 false。 By default is true.默认为真。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM